Cross-language Go program function calling method, program product, equipment and storage medium
By converting Python call requests into command-line arguments for Go programs through a cross-language executor, starting Go program subprocesses, and exchanging data through a unified result format structure, the problem of low efficiency in cross-language calls in existing technologies is solved, and efficient and reliable cross-language data transmission and business operation execution are achieved.
Patent Information
- Application Number
- CN202511720639.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-11-21
- Publication Date
- 2026-02-24
AI Technical Summary
Existing cross-language calling solutions, especially CGO binding solutions, suffer from low development efficiency and debugging difficulties. In particular, the cross-language calling needs between Python as the preferred language for automated testing frameworks and Go language infrastructure development in actual enterprise development processes have not been effectively resolved.
By using a cross-language executor to convert the call request into the corresponding command-line arguments of the Go program, starting the Go program subprocess, and calling the Go program function through the command processing function, data interaction is carried out using a unified result format structure, and a process-level isolated calling method and a standardized communication framework are adopted to reduce network protocol overhead and improve calling efficiency and ease of use.
It significantly improves the execution and development efficiency of cross-language calls, simplifies the data conversion process between different language environments, enhances call performance and convenience, and ensures the reliability and stability of data interaction.
Smart Images

Figure CN121560294A_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of computer technology, and more specifically, to a method, program product, device, and medium for cross-language invocation of Go program functions. Background Technology
[0002] With the widespread adoption of cloud-native architectures, Go has become dominant in the infrastructure field due to its high performance and concurrency advantages, with many middleware components (such as Redis and Docker) being developed using Go. However, in actual enterprise development processes, Python remains the preferred language for automated testing frameworks, creating a need for cross-language calls. Existing cross-language call solutions include CGO binding solutions, which require writing a C language middleware layer, resulting in low development efficiency and debugging difficulties. Summary of the Invention
[0003] The purpose of this application is to provide a method, program product, electronic device, and storage medium for cross-language invocation of Go program functions, in order to improve the problems of low development efficiency and debugging difficulties of the above-mentioned cross-language invocation scheme.
[0004] In a first aspect, embodiments of this application provide a method for cross-language invocation of Go program functions, comprising: receiving an invocation request initiated by a Python application; converting the invocation request into command-line arguments corresponding to the Go program through a cross-language executor; the command-line arguments include a function identifier and / or function parameters of the Go program function to be invoked; starting a Go program subprocess according to the command-line arguments through the cross-language executor and passing the command-line arguments to the Go program; parsing the command-line arguments through the Go program and determining a command processing function for processing the command-line arguments according to a command dispatch mechanism; passing the command-line arguments to the command processing function, invoking the Go program function through the command processing function, obtaining the operation result, encapsulating the operation result into a unified result format structure, and sending the unified result format structure to the Python application; the unified result format structure is a cross-language data interaction format between the Go program and the Python application.
[0005] In the above implementation process, a cross-language executor converts the call request into the corresponding command-line arguments of the Go program, thereby converting high-level language call requests into process instructions and reducing the work of manually constructing commands. By creating a Go program subprocess, an isolated and controllable execution environment is established. By establishing a standardized cross-language communication framework, the execution efficiency and development efficiency of cross-language calls are significantly improved. The process-level isolation calling method reduces the additional overhead of traditional network protocols (such as HTTP calls), the unified data exchange format simplifies the data conversion process between different language environments, and the dynamic command dispatch mechanism improves call performance, thus improving the convenience and efficiency of cross-language calls overall.
[0006] Optionally, in this embodiment of the application, the invocation request is converted into command-line parameters corresponding to the Go program by a cross-language executor, including: parsing the invocation request by the cross-language executor to obtain the function identifier and / or function parameters of the Go program function to be invoked; and generating command-line parameters corresponding to the Go program based on the function identifier and function parameters.
[0007] In the above implementation process, a reliable conversion was achieved from the high-level language used in the call request to the command-line parameters that the operating system can understand, thus establishing an accurate input foundation for subsequent inter-process calls.
[0008] Optionally, in this embodiment of the application, starting a Go program subprocess based on command-line parameters using a cross-language executor and passing the command-line parameters to the Go program includes: determining the file path of the Go program using the cross-language executor; obtaining the file path through relative path conversion or through a custom path; starting the Go program subprocess based on the file path of the Go program, using the command-line parameters as startup parameters, and passing the command-line parameters to the Go program; the Go program subprocess uses standard input and standard output.
[0009] In the above implementation process, the reliability and deployment flexibility of Go program file paths are improved by combining intelligent path resolution with custom configuration. By passing command-line arguments as startup parameters and utilizing operating system-level standard input / output streams for inter-process communication, a cross-language call channel with low latency and high stability, requiring no network protocol conversion, is built, thereby improving the execution efficiency and ease of cross-language calls.
[0010] Optionally, in this embodiment, parsing command-line arguments using a Go program and determining a command processing function to handle the arguments according to a command dispatch mechanism includes: verifying at least one received command-line argument using the main function of the Go program, and generating an initialization client instance if the verification is successful; matching the corresponding command processing function based on the command identifier in the command-line argument using the command dispatch mechanism; the command dispatch mechanism is used to map the command identifier to the command processing function; passing the command-line arguments to the command processing function, and calling the Go program function through the command processing function to obtain the operation result, including: passing the initialization client instance and the command-line arguments to the command processing function, and the command processing function using the initialization client instance to perform business operations and generate the operation result.
[0011] In the above implementation process, rigorous parameter validation improves the legitimacy of call requests and system stability, while an efficient command dispatch mechanism routes call requests to internal functional modules, significantly improving the overall execution efficiency of cross-language calls. By passing the initialized client instance along with specific parameters to the processing function, a complete context environment is provided for business operation execution, enabling the execution of business logic and improving the execution efficiency of business operations in cross-language calls.
[0012] Optionally, in this embodiment, encapsulating the operation result into a unified result format structure and sending the unified result format structure to the Python application includes: encapsulating the operation result into a unified result format structure according to a preset structure; the unified result format structure includes at least one of a status identifier, returned data, and error information; serializing the unified result format structure through a result sender to generate a standard sequence format, and outputting the standard sequence format through a standard output stream; the standard sequence format is a JSON string; sending the standard sequence format to the Python application so that the Python application receives the standard sequence format and obtains the application result corresponding to the call request according to the standard sequence format.
[0013] In the above implementation process, the standardized encapsulation of operation results is achieved through a pre-defined structure to ensure the structural uniformity of cross-language data interaction; JSON is used as the serialization format to improve the universality and efficiency of data exchange; a direct data transmission channel is established using the standard output stream to reduce the overhead of the network protocol stack; status indicators can more clearly observe the processing results, thereby improving the data transmission efficiency and processing reliability of cross-language calls.
[0014] Optionally, in this embodiment of the application, the process of generating the application result includes: the Python application receiving a standard sequence format, parsing the standard sequence format through a type-safe converter, extracting the JSON structure from the standard sequence format using regular expressions, and obtaining the application result corresponding to the call request.
[0015] In the above implementation process, precise extraction using regular expressions completely captures and separates structured data in the mixed output stream, reducing interference from non-data content in the parsing process. Deserialization enables reliable conversion and semantic restoration of data from the transmission format to application-layer objects, thereby improving the reliability, accuracy, and overall processing efficiency of cross-language calls during the result collection phase.
[0016] Optionally, in this embodiment of the application, parsing the standard sequence format using a type-safe converter includes: isolating errors of different exception types that occur during the parsing process; and handling errors of different exception types independently; exception types include parsing failure and / or subprocess timeout.
[0017] In the above implementation process, by establishing a hierarchical and isolated exception handling mechanism, the accurate identification and classification of errors of different natures are achieved; by providing independent handling logic for exceptions such as parsing failure and child process timeout, the ability to handle errors is improved, and the system can run stably even when errors occur, thereby effectively ensuring the reliability of cross-language call processes.
[0018] Optionally, in this embodiment, the unified result format structure includes a status identifier; after outputting the standard sequence format through the standard output stream, the method further includes: the Go program subprocess generating a differentiated exit code based on the status identifier in the unified result format structure, and terminating the process; wherein, if the status identifier indicates that the operation was successful, the differentiated exit code is a first value; if the status identifier indicates that the operation failed, the differentiated exit code is a second value.
[0019] In the above implementation process, by mapping the state of business operations to standardized process exit codes, the differentiated exit codes enable the parent process to quickly determine whether the child process's operation was successful without parsing business data, providing the caller with clear binary success / failure signals. Simultaneously, the process termination process enables efficient resource reclamation, reduces the generation of useless processes, and improves the reliability, simplicity, and execution efficiency of cross-language calls.
[0020] Optionally, in this embodiment of the application, after starting the Go program subprocess according to the command line parameters, the method further includes: performing timeout management on the Go program subprocess after starting through a cross-language executor; if no response is received from the Go program subprocess within a preset timeout period, the Go program subprocess is terminated and a timeout exception result is generated.
[0021] In the above implementation process, by using a timeout management mechanism, the resource consumption problem caused by process timeout can be reduced. By terminating processes that have timed out and releasing resources, the system's recovery capability can be improved, thereby enhancing the overall stability and resource utilization efficiency of the cross-language calling system.
[0022] Secondly, embodiments of this application also provide an apparatus for cross-language invocation of Go program functions, comprising: a request processing module, configured to receive an invocation request initiated by a Python application, and convert the invocation request into command-line parameters corresponding to the Go program through a cross-language executor; the command-line parameters include a function identifier and / or function parameters of the Go program function to be invoked; a subprocess module, configured to start a Go program subprocess according to the command-line parameters through the cross-language executor, and pass the command-line parameters to the Go program; a function allocation module, configured to parse the command-line parameters through the Go program, and determine the command processing function for processing the command-line parameters according to the command dispatch mechanism; a function invocation module, configured to pass the command-line parameters to the command processing function, invoke the Go program function through the command processing function, and obtain the operation result; and a format conversion module, configured to encapsulate the operation result into a unified result format structure, and send the unified result format structure to the Python application; the unified result format structure is a cross-language data interaction format between the Go program and the Python application.
[0023] Thirdly, embodiments of this application also provide a computer program product, including computer program instructions, which are executed by a processor to perform the method provided in the first aspect or any implementation thereof.
[0024] Fourthly, embodiments of this application also provide an electronic device, including: a processor and a memory, the memory storing computer program instructions, which are executed by the processor to perform the method provided in the first aspect or any implementation thereof.
[0025] Fifthly, embodiments of this application also provide a computer-readable storage medium storing computer program instructions, which, when executed by a processor, perform the method provided in the first aspect or any implementation thereof.
[0026] This application provides a method, program product, electronic device, and storage medium for cross-language invocation of Go program functions. By using a cross-language executor to convert invocation requests into corresponding command-line arguments for the Go program, it achieves the conversion of high-level language invocation requests into process instructions, reducing the work of manually constructing commands. By creating Go program subprocesses, an isolated and controllable execution environment is established. By establishing a standardized cross-language communication framework, the execution and development efficiency of cross-language invocations are significantly improved. The process-level isolation invocation method reduces the additional overhead of traditional network protocols (such as HTTP calls), the unified data exchange format simplifies the data conversion process between different language environments, and the dynamic command distribution mechanism improves invocation performance, thereby improving the overall convenience and efficiency of cross-language invocations. Attached Figure Description
[0027] To more clearly illustrate the technical solutions of the embodiments of this application, the accompanying drawings used in the embodiments of this application will be briefly introduced below. It should be understood that the following drawings only show some embodiments of this application and should not be regarded as a limitation of the scope. For those skilled in the art, other related drawings can be obtained based on these drawings without creative effort.
[0028] Figure 1 A flowchart illustrating a method for cross-language invocation of Go program functions, provided as an embodiment of this application; Figure 2 A schematic diagram of the structure of the apparatus for cross-language invocation of Go program functions provided in the embodiments of this application; Figure 3 This is a schematic diagram of the structure of an electronic device provided in an embodiment of this application. Detailed Implementation
[0029] The embodiments of the technical solution of this application will now be described in detail with reference to the accompanying drawings. These embodiments are only used to more clearly illustrate the technical solution of this application and are therefore merely examples, and should not be used to limit the scope of protection of this application.
[0030] Unless otherwise defined, all technical and scientific terms used herein have the same meaning as commonly understood by one of ordinary skill in the art to which this application belongs; the terminology used herein is for the purpose of describing particular embodiments only and is not intended to limit this application.
[0031] In the description of the embodiments of this application, technical terms such as "first" and "second" are used only to distinguish different objects and should not be construed as indicating or implying relative importance or implicitly specifying the number, specific order, or primary and secondary relationship of the indicated technical features. In the description of the embodiments of this application, "multiple" means two or more, unless otherwise explicitly defined.
[0032] Existing cross-language call or cross-language testing solutions include the following categories: (1) REST API encapsulation solution, which exposes Go functionality through HTTP service, resulting in significant call latency and severe call time consumption. (2) CGO binding solution, which requires writing a C language middleware layer, increasing the development workload significantly. (3) GRPC cross-language call, which has high serialization overhead and significant performance loss in testing.
[0033] This application provides a method for cross-language invocation of Go program functions. A cross-language executor converts the invocation request into command-line arguments corresponding to the Go program, thereby converting high-level language invocation requests into process instructions and reducing the manual command construction work. By creating a Go program subprocess, an isolated and controllable execution environment is established. By establishing a standardized cross-language communication framework, the execution and development efficiency of cross-language invocations are significantly improved. The process-level isolation invocation method reduces the overhead of traditional network protocols (such as HTTP calls), the unified data exchange format simplifies the data conversion process between different language environments, and the dynamic command distribution mechanism improves invocation performance, thus improving the overall convenience and efficiency of cross-language invocations.
[0034] Please see Figure 1 The illustrated diagram shows a flowchart of a method for cross-language invocation of Go program functions according to an embodiment of this application. This method for cross-language invocation of Go program functions can be applied to electronic devices, which may include physical devices such as servers, PCs, tablets, or smartphones, or virtual devices such as virtual machines or containers. The electronic device can be a single device, a combination of multiple devices, or a cluster of a large number of devices. The method for cross-language invocation of Go program functions may include: Step S110: Receive the call request initiated by the Python application, and convert the call request into the corresponding command line parameters of the Go program through the cross-language executor; the command line parameters include the function identifier and / or function parameters of the Go program function to be called.
[0035] Step S120: Start the Go program subprocess based on the command-line arguments using the cross-language executor, and pass the command-line arguments to the Go program.
[0036] Step S130: Parse the command-line arguments using the Go program and determine the command processing function to handle the command-line arguments based on the command dispatch mechanism.
[0037] Step S140: Pass the command line arguments to the command processing function, call the Go program function through the command processing function, and obtain the operation result.
[0038] Step S150: Encapsulate the operation result into a unified result format structure and send the unified result format structure to the Python application; the unified result format structure is a cross-language data exchange format between the Go program and the Python application.
[0039] In step S110, a call request refers to an instruction issued by a Python application to perform a specific Go language function, which includes a function identifier and / or function parameters of the Go program function; the function identifier is, for example, a command name, and the function parameters are, for example, the input data required to perform the function.
[0040] The cross-language executor is a component that runs in a Python environment and is responsible for coordinating the entire cross-language call process. The cross-language executor first receives and parses the call request, and then, according to predefined rules, translates the call request into command-line arguments that are recognizable at the operating system level. These command-line arguments can be viewed as instructions for calling functions of the Go program.
[0041] The conversion process is as follows: The cross-language executor first parses the function identifier in the call request and uses it as the main command of the command-line arguments; then, it extracts the function arguments in sequence and converts them into additional options or parameter values for the command-line arguments. This step achieves the serialization conversion from the call request (Python object) to the command-line arguments (string arguments), ensuring that various data types can be correctly encoded into command-line text, thus establishing a standardized input foundation for subsequent inter-process calls.
[0042] In step S120, the Go program subprocess refers to a Go program instance created and run independently by the Python main process, which can be created through the process management mechanism provided by the operating system. The cross-language executor performs process management in step S120. When the cross-language executor executes, it first locates the storage path of the Go program binary file, starts a new process instance through a system call, and then passes the converted command-line arguments as startup parameters to the newly created process.
[0043] This process fully utilizes the process isolation features of the operating system to ensure that the Go program's runtime environment is completely separated from the Python main process. At the same time, through the standard inter-process parameter passing mechanism, the calling instructions can be accurately delivered to the Go program's entry point, providing a reliable execution environment for cross-language function calls.
[0044] In step S130, the command dispatch mechanism is a routing logic implemented internally by the Go program, responsible for mapping different command-line instructions to corresponding processing functions. The Go program extracts the main command identifier, such as a function identifier, from the received command-line arguments and uses predefined routing rules to match and search for the command processing function used to process the command-line arguments. Routing rules include command mapping tables or conditional judgment logic, etc. This process establishes the correspondence between command-line arguments and specific business functions. As one implementation method, the integrity and validity of the parameters can be verified before matching command line parameters to determine whether the parameters or conditions required by the command processing function can be met.
[0045] In step S140, the command processing function is a code unit in the Go program that specifically implements the business logic. Each function is dedicated to handling a specific type of function request. The command processing function receives verified command-line arguments, parses out the specific parameter values required for the business operation, and then calls the corresponding native Go language function, i.e., the Go program function, to perform the actual data processing, system operation, or service call. After executing the function, a raw "operation result" is generated, which may be a successful return value or an error message indicating a failed call.
[0046] As one implementation method, during the process of the command processing function calling the Go program's functions, the command processing function can also detect and handle abnormal situations, and errors can be captured and generated in a timely manner during runtime.
[0047] In step S150, after obtaining the original operation result, the Go program does not output it directly. Instead, it encapsulates the result using a predefined, standardized unified result format structure (e.g., CommandResult). The unified result format structure is a cross-language data exchange format between the Go program and the Python application; the unified result format structure includes at least one of the following: status flag, returned data, and error message.
[0048] The Go program then serializes the unified result format structure to generate a standard sequence format, and outputs the standard sequence format through the standard output stream. After outputting the standard sequence format, the Go program generates an exit code indicating success or failure and terminates the process. The standard sequence format is then sent to the Python application, which receives it.
[0049] By constructing a unified result format structure and serializing the unified result format structure, complex, typed Go language data is transformed into a universal format that Python applications can easily parse, greatly simplifying the complexity of cross-language data exchange, ensuring the reliability and consistency of communication, and thus significantly improving the efficiency and reliability of cross-language calls.
[0050] In the implementation of the above embodiments: the cross-language executor converts the call request into the command-line parameters corresponding to the Go program, thereby converting the high-level language call request into process instructions and reducing the work of manually constructing commands. By creating a Go program subprocess, an isolated and controllable execution environment is established. By establishing a standardized cross-language communication framework, the execution efficiency and development efficiency of cross-language calls are significantly improved. The process-level isolation calling method reduces the additional overhead of traditional network protocols (such as HTTP calls), the unified data exchange format simplifies the data conversion process between different language environments, and the command distribution mechanism improves call performance, thus improving the convenience and efficiency of cross-language calls overall.
[0051] Optionally, in this embodiment of the application, the call request is converted into command-line arguments corresponding to the Go program through a cross-language executor, including: The cross-language executor parses the call request to obtain the function identifier and / or function parameters of the Go program function to be called; based on the function identifier and function parameters, the corresponding command-line parameters of the Go program are generated.
[0052] Function identifiers uniquely identify the Go program function to be invoked, such as a specific Redis operation command; function parameters are auxiliary information necessary to execute the function, such as database key names or values. A cross-language executor is a functional module in the Python environment responsible for interacting with external programs, its function being to eliminate syntactic and interaction differences between different programming languages.
[0053] The cross-language executor first performs deep parsing on the received call request, accurately extracting the function identifier and ordered list of function parameters from the request structure. Then, the extracted parameters can be assembled: for example, the extracted function identifier is used as the first independent parameter in the command line parameter sequence, which constitutes the main command of the call; each function parameter is converted into subsequent parameter items in the command line according to its order in the request.
[0054] For non-string parameters, the cross-language executor automatically performs type conversion and serialization, ensuring that complex data structures such as numbers and lists can be accurately encoded into command-line argument format. This process achieves reliable conversion from built-in data structures of high-level languages to standardized string instructions at the operating system level, establishing a precise and unambiguous input foundation for subsequent inter-process calls.
[0055] In the implementation of the above embodiments, a reliable conversion was achieved between the high-level language used in the call request and the command-line parameters that the operating system can understand, thus establishing an accurate input foundation for subsequent inter-process calls.
[0056] Optionally, in this embodiment of the application, a Go program subprocess is started based on command-line arguments using a cross-language executor, and the command-line arguments are passed to the Go program, including: The cross-language executor determines the file path of the Go program; the file path is obtained through relative path conversion or through a custom path. The file path of the Go program points to the location information of the compiled Go executable binary file in the operating system's storage system. The cross-language executor can obtain the file path in two ways: The first method receives and resolves custom paths explicitly specified by the caller, providing flexibility for deployment. The second method receives relative paths and automatically converts them to absolute paths based on the current working directory using the system path resolution library. This process eliminates path ambiguity and improves the determinism of program addressing.
[0057] Based on the file path of the Go program, the command-line arguments are used as startup parameters to start a Go program subprocess, which then passes the command-line arguments to the Go program. The Go program subprocess uses standard input and standard output.
[0058] After obtaining the file path of the Go program, the cross-language executor starts the process creation process. It can call the native process management interface provided by the operating system, using the determined Go program file path as the main program to be executed and the command-line arguments as the program's startup parameters. This configuration information is used to create a brand new Go program child process. A Go program child process is an independent execution instance scheduled by the operating system, running in parallel with the Python main process, and isolated from it.
[0059] In an optional embodiment, when a process is created, the cross-language executor explicitly configures and takes over the standard input and standard output pipes of the child process. Standard input and standard output are basic communication channels pre-defined by the operating system for each process to exchange text data with the external environment. Standard input is the channel for the process to receive data, and standard output is the channel for the process to send data. In this way, a standard-based, efficient inter-process communication link is established between the cross-language executor and the Go program child process, laying a solid foundation for the subsequent transmission of structured data.
[0060] In the implementation of the above embodiments: by combining intelligent path resolution with custom configuration, the reliability and deployment flexibility of Go program file paths are improved. By passing command-line arguments as startup parameters and utilizing operating system-level standard input / output streams for inter-process communication, a cross-language call channel that requires no network protocol conversion, has low latency, and is highly stable is constructed, thereby improving the execution efficiency and simplicity of cross-language calls.
[0061] Optionally, in this embodiment, the command-line arguments are parsed by a Go program, and the command processing function for processing the command-line arguments is determined according to the command dispatch mechanism, including: The Go program's main function validates at least one received command-line argument. If validation passes, it generates an initialized client instance. The Go program's main function is the standardized entry point for Go applications; it is automatically executed first when the operating system starts a Go program child process. Command-line arguments are string arrays passed to the main function by the operating system, containing function identifiers and parameters passed from the Python side.
[0062] Before the main function executes, a verification process can be performed: by checking the length and basic format of the command-line argument array, it ensures that at least one valid command identifier is provided and that the parameter structure meets the requirements, thereby reducing the possibility of subsequent processes terminating abnormally due to invalid input. If the verification passes, the system will execute the initialization client instance creation process. This refers to creating and establishing a connection object with the target external service (such as a Redis database) based on pre-configured connection parameters (such as server address, authentication information, etc.). This instance will become the bridge for all subsequent business operations and interactions with the external service.
[0063] The command dispatch mechanism matches the corresponding command processing function based on the command identifier in the command-line arguments; the command dispatch mechanism is used to map command identifiers to command processing functions. The command dispatch mechanism can be implemented through a predefined command-function mapping table or conditional branching logic (such as a switch statement).
[0064] For example, the first parameter from the verified command-line arguments is extracted as a command identifier. This identifier is then used to find the exact matching command processing function within the mapping structure. Command processing functions are pre-packaged code units in a Go program designed to perform specific business functions. Through this mapping and matching process, the system can accurately route external call requests to the code module responsible for handling that specific function, improving operational efficiency.
[0065] Command-line arguments are passed to the command processing function, which then calls the Go program's functionality to obtain the operation results, including: The initialization client instance and command-line arguments are passed to the command processing function, which uses the initialization client instance to perform business operations and generate operation results.
[0066] The initialization client instance is a connection object ready to communicate with external services, encapsulating all necessary network connections and authentication status. The input process refers to passing the initialization client instance and parsed command-line arguments as input parameters to the command processing function when it is invoked. This allows the command processing function to simultaneously possess the context connection and specific instructions required to perform the operation.
[0067] Upon receiving these inputs, the command processing function begins executing business operations. Executing business operations means that the function uses the passed-in initialized client instance to call its provided application programming interface (API), sends specific operation commands (such as data querying or writing) to the corresponding external service, and synchronously waits for the server's response. Generating the operation result means that the command processing function, based on the raw response data returned by the external service and the operation execution status (success or failure), constructs a structured, standardized data object containing an operation status identifier, a returned data entity, or error information.
[0068] In the implementation of the above embodiments: rigorous parameter verification improves the legality of call requests and system stability; an efficient command dispatch mechanism routes call requests to internal functional modules, thereby significantly improving the overall execution efficiency of cross-language calls; and by passing the initialized client instance along with specific parameters to the processing function, a complete context environment is provided for business operation execution, enabling the execution of business logic and improving the execution efficiency of business operations in cross-language calls.
[0069] Optionally, in this embodiment, the operation result is encapsulated into a unified result format structure, and the unified result format structure is sent to the Python application, including: Based on a predefined structure, the operation result is encapsulated into a unified result format structure. This unified result format structure includes at least one of the following: status flag, return data, and error message. The predefined structure refers to a standardized data container explicitly defined in a Go program, whose field composition and data types are determined during the program design phase. The encapsulation process refers to the transformation operation of standardizing, reorganizing, and filling the original operation result according to the specifications of the predefined structure.
[0070] The unified result format structure is generated through encapsulation and can contain a composite data structure with at least one core field: a status flag, a boolean flag indicating the success or failure of the operation; a return data field, capable of holding data of any type, storing the payload to be returned upon successful operation; and an error message field, a string field recording the specific error description upon failure. This structured encapsulation improves the consistency of the representation of results from different operations.
[0071] The result sender serializes the uniform result format structure, generating a standard sequence format, and outputs the standard sequence format as a JSON string through the standard output stream. The result sender is the functional module in a Go program responsible for data serialization and output. Serialization refers to the process of converting a uniform result format structure (an in-memory object) into a standardized sequence of bytes that can be stored or transmitted. The standard sequence format refers to the JSON string format, a lightweight, cross-platform, cross-language data exchange format characterized by its hierarchical structure, high readability, and high parsing efficiency.
[0072] The result sender uses a JSON encoding library to convert all fields and their values of the uniform result format structure instance into their corresponding string representations according to the JSON specification. The system then outputs this JSON string through the standard output stream. The standard output stream is a standard text output channel provided by the operating system for each process, typically used to transmit normal execution results to the external environment, and can also be used for cross-language data transmission.
[0073] Send a standard sequence format to the Python application so that the Python application receives the standard sequence format and obtains the application result corresponding to the call request based on the standard sequence format.
[0074] The Go program performs I / O operations to write the generated standard sequence format (JSON string) to its standard output stream. The Python application, acting as the parent process, continuously listens for and captures the content of the Go program's child process's standard output stream. Receiving refers to the Python program's behavior of reading the complete standard sequence format from the child process's output stream.
[0075] The Python application then calls its JSON parsing library to deserialize the JSON string into a native Python dictionary object. Based on the status field in the standard sequence format, the Python program can accurately determine the status of the operation result: if the status is successful, it extracts valid information from the returned data field and converts it into the corresponding Python object; if the status is failed, it constructs and throws the corresponding exception based on the error information field.
[0076] In the implementation of the above embodiments: the standardized encapsulation of operation results is achieved through a preset structure, ensuring the structural uniformity of cross-language data interaction; JSON is used as the serialization format to improve the universality and efficiency of data exchange; a direct data transmission channel is established using the standard output stream, reducing the overhead of the network protocol stack; status indicators can more clearly observe the processing results, thereby improving the data transmission efficiency and processing reliability of cross-language calls.
[0077] In an optional embodiment, this application embodiment can establish a standard input stream as a continuous command receiving channel in addition to the standard output stream when the Go program subprocess starts. The Python application can encapsulate the command-line arguments corresponding to multiple associated operations into a streaming command sequence and continuously pass it to the Go program. The Go program maintains the session state, processes the streaming command sequence sequentially, and returns the sequence results in a streaming manner. This solution can be used for scenarios such as batch processing of large datasets and reporting progress of long-term tasks, completing complex workflows with the overhead of a single process startup, and significantly reducing the frequency of process creation.
[0078] Optionally, in this embodiment, the process of generating the application result includes: the Python application receives a standard sequence format, parses the standard sequence format using a type-safe converter, extracts the JSON structure from the standard sequence format using regular expressions, and obtains the application result corresponding to the call request.
[0079] Type-safe converters are Python modules specifically designed for data extraction and deserialization. They are used to accurately separate valid structured data from text streams that may contain noise, and reliably convert it into Python's built-in data types.
[0080] The type-safe converter first initiates a regular expression-based extraction process. Regular expressions are pattern recognition tools used for string matching, such as recognizing the complete structure of a JSON object.
[0081] In this embodiment, the type-safe converter is configured to recognize JSON structures enclosed in curly braces. For example, the regular expression is applied to the entire original output text, performing a line-by-line scan to locate and extract one or more complete JSON string fragments enclosed in curly braces. This step effectively isolates other non-JSON text that the Go program may output, improving the input purity of the parsing process.
[0082] After successfully extracting the JSON string, the type-safe converter calls Python's JSON parsing library to deserialize the string into a Python dictionary object. This dictionary object perfectly corresponds to the unified result format structure sent by the Go client, containing key-value pairs such as status flags, returned data, and error messages. The type-safe converter can check the status flag keys in the dictionary: if the flag indicates success, the value corresponding to the returned data key is returned as the valid application result of this call; if the flag indicates failure, a clear business exception is constructed and thrown based on the value of the error message key. The entire parsing process is encapsulated in layered exception handling logic, which can capture JSON parsing errors, business logic errors, etc., ensuring that each type of error is handled appropriately.
[0083] In the implementation of the above embodiments: Regular expression-based precise extraction technology is used to completely capture and separate structured data in the mixed output stream, reducing interference from non-data content in the parsing process. Deserialization enables reliable conversion and semantic restoration of data from the transmission format to application-layer objects, thereby improving the reliability, accuracy, and overall processing efficiency of cross-language calls during the result collection phase.
[0084] Optionally, in this embodiment of the application, parsing the standard sequence format using a type-safe converter includes: During the parsing of standard sequence formats using a type-safe converter, errors of different exception types that occur during the parsing process are isolated; and different exception types are handled independently; exception types include parsing failure and / or subprocess timeout.
[0085] During the process of parsing standard sequence formats using a type-safe converter, the system needs to handle various possible exceptions. The parsing process refers to the complete workflow of converting the received standard sequence format (JSON string) into a Python-usable data object.
[0086] A parsing failure exception refers to an error situation where the JSON string format is not compliant, contains syntax errors, or is structurally corrupted, causing the system to be unable to correctly deserialize it into a Python dictionary object; a child process timeout exception refers to the failure to successfully obtain any valid data returned by the Go program's child process through the standard output stream within the preset waiting time, which may be caused by reasons such as the child process freezing.
[0087] Isolating different types of errors involves establishing independent capture channels and handling logic for different types of exceptions during the program design phase. For example, a layered exception capture mechanism can be used, with multiple independent exception capture code blocks to intercept different types of exceptions. When a parsing failure exception occurs, the system will enter a logic branch specifically for handling data format errors. This branch will discard invalid JSON data and generate and return a clear "data format error" message.
[0088] When a child process timeout exception is detected, the system enters a dedicated logic branch for handling inter-process communication timeouts. This branch proactively terminates the timed-out child process to release system resources and generates an "Operation Timeout" error message. In one implementation, a corresponding retry mechanism or alarm process can be triggered in the event of this error.
[0089] In the implementation of the above embodiments: by establishing a hierarchical and isolated exception capture mechanism, accurate identification and classification of errors of different natures are achieved; by providing independent processing logic for exceptions such as parsing failure and child process timeout, the ability to handle errors is improved, and the system can run stably even when errors occur, thereby effectively ensuring the reliability of cross-language call processes.
[0090] Optionally, in this embodiment, the unified result format structure includes a status flag; after outputting the standard sequence format via the standard output stream, the method further includes: Go program subprocesses generate differentiated exit codes based on the status flags in the unified result format structure and then terminate the process. Specifically, if the status flag indicates that the operation was successful, the differentiated exit code is the first value; if the status flag indicates that the operation failed, the differentiated exit code is the second value.
[0091] The status flag in the unified result format structure is a boolean flag generated after the Go program completes its business operation. It indicates whether the operation was successful. Before the Go program's child process terminates, it reads the status flag field from the encapsulated unified result format structure. If the status flag indicates success, the program calls the system exit instruction, carrying the first value as the exit code. This first value can be set to the integer 0, or any other value.
[0092] Conversely, if the status flag indicates that the operation has failed, the program calls the system exit instruction with a non-zero second value as the exit code. This second value can usually be set to the integer 1 or other non-zero integer to explicitly indicate "abnormal exit". After generating the exit code, the Go program subprocess executes the termination process, releases all the memory and other system resources it occupies, and the operating system kernel completes the final process cleanup.
[0093] In the implementation of the above embodiments: by mapping the state of business operations to standardized process exit codes, the differentiated exit codes enable the parent process to quickly determine whether the child process has successfully executed the operation without parsing business data, providing the caller with clear binary success / failure signals. Simultaneously, the process termination process achieves efficient resource reclamation, reduces the generation of useless processes, and improves the reliability, simplicity, and execution efficiency of cross-language calls.
[0094] Optionally, in this embodiment of the application, after starting the Go program subprocess according to the command-line parameters, the method further includes: The Go program subprocess is managed by a cross-language executor. If no response is received from the Go program subprocess within the preset timeout period, the Go program subprocess is terminated and a timeout exception result is generated.
[0095] Timeout management prevents indefinite waiting due to abnormal execution of child processes. The preset time is the maximum allowed waiting time from the start of the child process to the expected receipt of its complete response. The time can be set according to the complexity of business logic and system performance requirements, such as 50 seconds, 60 seconds or 70 seconds.
[0096] After successfully creating a Go program child process, the cross-language executor synchronously starts a separate timeout timer. This timer runs asynchronously in the background, parallel to the execution of the child process. While waiting for the child process to complete naturally and capturing its output, the cross-language executor monitors the timeout timer's status. If a response is successfully received within the preset timeout period, the timer is cleared, and the process continues normally. Conversely, if the timer triggers before the response arrives, it indicates a timeout has occurred, and the execution of the Go program child process can be terminated, reclaiming the system resources it occupied. In case of failure, a timeout exception result can be generated, including a description of the timeout event, the occurrence time, and other diagnostic data.
[0097] In the implementation of the above embodiments: by using a timeout management mechanism, the resource occupation problem caused by process timeout is reduced, the process that timed out is terminated, and resources are released, which can improve the system's recovery capability and thus improve the overall stability and resource utilization efficiency of the cross-language calling system.
[0098] Please see Figure 2 The diagram shown is a structural schematic of a device for cross-language invocation of Go program functions provided in an embodiment of this application; this application provides a device 200 for cross-language invocation of Go program functions, including: The request processing module 210 is used to receive the call request initiated by the Python application and convert the call request into the corresponding command line parameters of the Go program through the cross-language executor; the command line parameters include the function identifier and / or function parameters of the Go program function to be called; Subprocess module 220 is used to start a Go program subprocess based on command-line arguments through a cross-language executor and pass the command-line arguments to the Go program; The function allocation module 230 is used to parse command-line arguments through the Go program and determine the command processing function to process the command-line arguments according to the command dispatch mechanism. Function call module 240 is used to pass command-line arguments to a command processing function, which then calls the Go program's functionality to obtain the operation result. The format conversion module 250 is used to encapsulate the operation results into a unified result format structure and send the unified result format structure to the Python application; the unified result format structure is a cross-language data exchange format between the Go program and the Python application.
[0099] Optionally, in this embodiment of the application, the device for cross-language invocation of Go program functions includes a request processing module 210, which is used to parse the invocation request through a cross-language executor to obtain the function identifier and / or function parameters of the Go program function to be invoked; and to generate command line parameters corresponding to the Go program based on the function identifier and function parameters.
[0100] Optionally, in this embodiment of the application, the subprocess module 220 of the device for cross-language invocation of Go program functions is used to determine the file path of the Go program through a cross-language executor; the file path is obtained through relative path conversion or through a custom path; based on the file path of the Go program, the command line parameters are used as startup parameters to start the Go program subprocess and pass the command line parameters to the Go program; the Go program subprocess uses standard input and standard output.
[0101] Optionally, in this embodiment of the application, the apparatus for cross-language invocation of Go program functions includes a function allocation module 230, which is used to verify at least one received command-line parameter through the main function of the Go program, and generate an initialization client instance if the verification is successful; and to match the corresponding command processing function according to the command identifier in the command-line parameter through a command dispatch mechanism; the command dispatch mechanism is used to map the command identifier to the command processing function; and a function invocation module 240, which is used to pass the initialization client instance and the command-line parameter to the command processing function, and the command processing function uses the initialization client instance to perform business operations and generate operation results.
[0102] Optionally, in this embodiment of the application, the device for cross-language invocation of Go program functions includes a format conversion module 250, which is used to encapsulate the operation result into a unified result format structure according to a preset structure; the unified result format structure includes at least one of a status identifier, returned data, and error information; the unified result format structure is serialized by a result sender to generate a standard sequence format, and the standard sequence format is output through a standard output stream; the standard sequence format is a JSON string; the standard sequence format is sent to a Python application so that the Python application receives the standard sequence format and obtains the application result corresponding to the invocation request according to the standard sequence format.
[0103] Optionally, in this embodiment of the application, the apparatus for cross-language invocation of Go program functions includes the following process for generating application results: a Python application receives a standard sequence format, parses the standard sequence format using a type-safe converter, extracts the JSON structure from the standard sequence format using regular expressions, and obtains the application result corresponding to the invocation request.
[0104] Optionally, in this embodiment of the application, the device for cross-language invocation of Go program functions, the format conversion module 250, is used to isolate errors of different exception types that occur during the parsing process of parsing the standard sequence format through the type-safe converter; and to handle the errors of different exception types independently; the exception types include parsing failure and / or child process timeout.
[0105] Optionally, in this embodiment of the application, the apparatus for cross-language invocation of Go program functions further includes a differential exit code module, which is used for the Go program subprocess to generate a differential exit code based on the status identifier in the unified result format structure and terminate the process; wherein, if the status identifier indicates that the operation is successful, the differential exit code is a first value; if the status identifier indicates that the operation is unsuccessful, the differential exit code is a second value.
[0106] Optionally, in this embodiment of the application, the device for cross-language invocation of Go program functions further includes a timeout management module, which is used to perform timeout management after the Go program subprocess is started through the cross-language executor. If no response is received from the Go program subprocess within a preset timeout period, the Go program subprocess is terminated and a timeout exception result is generated.
[0107] It should be understood that this device corresponds to the above-described method embodiment for cross-language invocation of Go program functions, and is capable of executing the various steps involved in the above method embodiment. The specific functions of this device can be found in the description above, and detailed descriptions are omitted here to avoid repetition. The device includes at least one software function module that can be stored in memory or embedded in the device's operating system (OS) in the form of software or firmware.
[0108] Please see Figure 3 The diagram shows a structural schematic of an electronic device provided in an embodiment of this application. An electronic device 300 provided in this application includes a processor 310 and a memory 320. The memory 320 stores machine-readable instructions executable by the processor 310. When the machine-readable instructions are executed by the processor 310, the method described above is performed.
[0109] Figure 3 The components shown can be implemented using hardware, software, or a combination thereof. Electronic device 300 may be a physical device, such as a server or PC, or a virtual device, such as a virtual machine or virtualization container. Furthermore, electronic device 300 is not limited to a single device; it can be a combination of multiple devices or a cluster of numerous devices.
[0110] This application also provides a storage medium storing a computer program, which is executed by a processor to perform the above-described method.
[0111] The storage medium can be implemented by any type of volatile or non-volatile storage device or a combination thereof, such as Static Random Access Memory (SRAM), Electrically Erasable Programmable Read-Only Memory (EEPROM), Erasable Programmable Read Only Memory (EPROM), Programmable Red-Only Memory (PROM), Read-Only Memory (ROM), magnetic storage, flash memory, magnetic disk, or optical disk.
[0112] This application also provides a computer program product, including computer program instructions, which are executed by a processor to perform the method described above.
[0113] It should be understood that the disclosed apparatus and methods can also be implemented in other ways, given the several embodiments provided in this application. The apparatus embodiments described above are merely illustrative. For example, the flowcharts and block diagrams in the accompanying drawings illustrate the architecture, functionality, and operation of possible implementations of apparatus, methods, and computer program products according to various embodiments of this application. In this regard, each block in a flowchart or block diagram may represent a module, segment, or portion of code, which contains one or more executable instructions for implementing a specified logical function. It should also be noted that in some alternative implementations, the functions marked in the blocks may occur in a different order than those marked in the drawings. For example, two consecutive blocks may actually be executed substantially in parallel, or they may sometimes be executed in reverse order, depending on the functions involved. It should also be noted that each block in a block diagram and / or flowchart, and combinations of blocks in block diagrams and / or flowcharts, can be implemented using a dedicated hardware-based system that performs the specified function or action, or using a combination of dedicated hardware and computer instructions.
[0114] In addition, the functional modules in the various embodiments of this application can be integrated together to form an independent part, or each module can exist independently, or two or more modules can be integrated to form an independent part.
[0115] The above description is only an optional implementation of the embodiments of this application, but the protection scope of the embodiments of this application is not limited thereto. Any changes or substitutions that can be easily conceived by those skilled in the art within the technical scope disclosed in the embodiments of this application should be covered within the protection scope of the embodiments of this application.
Claims
1. A method for calling Go program functions across languages, characterized in that, include: It receives call requests initiated by Python applications and converts the call requests into command-line arguments for the Go program through a cross-language executor. The command-line parameters include the function identifier and / or function parameters of the Go program function to be invoked; The cross-language executor starts a Go program subprocess based on the command-line parameters and passes the command-line parameters to the Go program. The Go program parses the command-line arguments and determines the command processing function to handle the command-line arguments based on the command dispatch mechanism. The command-line arguments are passed to the command processing function, which then invokes the Go program's functionality to obtain the operation result. The operation result is encapsulated into a unified result format structure, and the unified result format structure is sent to the Python application; the unified result format structure is a cross-language data interaction format between the Go program and the Python application.
2. The method according to claim 1, characterized in that, The call request is converted into command-line arguments for the Go program using a cross-language executor, including: The cross-language executor parses the call request to obtain the function identifier and / or function parameters of the Go program function to be called; Based on the function identifier and function parameters, generate the command-line parameters corresponding to the Go program.
3. The method according to claim 1, characterized in that, The cross-language executor starts a Go program subprocess based on the command-line arguments and passes the command-line arguments to the Go program, including: The cross-language executor determines the file path of the Go program; the file path is obtained through relative path conversion or through a custom path. Based on the file path of the Go program, the command-line arguments are used as startup parameters to start the Go program subprocess, and the command-line arguments are passed to the Go program; the Go program subprocess uses standard input and standard output.
4. The method according to claim 1, characterized in that, The Go program parses the command-line arguments and, based on the command dispatch mechanism, determines the command processing function to handle the command-line arguments, including: The main function of the Go program verifies at least one of the received command-line arguments, and if the verification passes, generates an initial client instance. The command dispatch mechanism matches the corresponding command processing function based on the command identifier in the command line parameters; the command dispatch mechanism is used to map the command identifier to the command processing function. The command-line arguments are passed to the command processing function, which then calls the Go program's functionality to obtain the operation result, including: The initialization client instance and the command-line parameters are passed to the command processing function, which uses the initialization client instance to perform business operations and generate the operation results.
5. The method according to claim 1, characterized in that, Encapsulating the operation result into a unified result format structure and sending the unified result format structure to the Python application includes: According to the preset structure, the operation result is encapsulated into a unified result format structure; the unified result format structure includes at least one of status identifier, return data, and error information; The unified result format structure is serialized by the result sender to generate a standard sequence format, and the standard sequence format is output through the standard output stream; the standard sequence format is a JSON string. The standard sequence format is sent to the Python application so that the Python application receives the standard sequence format and obtains the application result corresponding to the call request based on the standard sequence format.
6. The method according to claim 5, characterized in that, in, The process of generating the application result includes: the Python application receiving the standard sequence format, parsing the standard sequence format through a type-safe converter, extracting the JSON structure from the standard sequence format using regular expressions, and obtaining the application result corresponding to the call request.
7. The method according to claim 6, characterized in that, Parsing the standard sequence format using a type-safe converter includes: During the parsing of the standard sequence format by the type-safe converter, errors of different exception types that occur during the parsing process are isolated; and the errors of different exception types are handled independently; the exception types include parsing failure and / or subprocess timeout.
8. The method according to claim 5, characterized in that, The unified result format structure includes a status identifier; after outputting the standard sequence format via the standard output stream, the method further includes: The Go program subprocess generates a differentiated exit code based on the status identifier in the unified result format structure and terminates the process; wherein, if the status identifier indicates that the operation is successful, the differentiated exit code is a first value; if the status identifier indicates that the operation is unsuccessful, the differentiated exit code is a second value.
9. The method according to any one of claims 1-8, characterized in that, After starting the Go program subprocess according to the command-line parameters, the method further includes: The cross-language executor performs timeout management after starting the Go program subprocess. If no response is received from the Go program subprocess within the preset timeout period, the Go program subprocess is terminated and a timeout exception result is generated.
10. A computer program product, characterized in that, It includes computer program instructions that are executed by a processor to perform the method as described in any one of claims 1 to 9.
11. An electronic device, characterized in that, include: A processor and a memory, the memory storing computer program instructions that, when executed by the processor, perform the method as described in any one of claims 1 to 9.
12. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores computer program instructions that, when executed by a processor, perform the method as described in any one of claims 1 to 9.