Home >Backend Development >Golang >Why is my Go program not receiving the return value from my Python function call?

Why is my Go program not receiving the return value from my Python function call?

Barbara Streisand
Barbara StreisandOriginal
2024-12-04 22:20:12792browse

Why is my Go program not receiving the return value from my Python function call?

Calling Python Functions from Go and Retrieving Return Values

In this scenario, you aim to call a Python function named cat_strings from a Go program and access its return value. However, despite attempting to execute the command, you encounter an issue where the expected return data is missing.

To resolve this issue, consider removing the single quotes around the Python command itself. Instead of:

program := "python"
arg0 := "-c"
arg1 := fmt.Sprintf("'import pythonfile; print pythonfile.cat_strings(\"%s\", \"%s\")'", "foo", "bar")

Use:

cmd := exec.Command("python",  "-c", "import pythonfile; print pythonfile.cat_strings('foo', 'bar')")

This adjustment ensures that the Python command is properly interpreted by the system, leading to the correct execution of your Python function. Consequently, you should be able to obtain the return value from the cat_strings function in your Go program.

The above is the detailed content of Why is my Go program not receiving the return value from my Python function call?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn