This README provides step-by-step instructions on how to run a Go function from a Python script. We'll create a minimal Go project with a function and a Python script to execute that function.
Before proceeding, make sure you have the following installed on your system:
- Go programming language (Download Go)
- Python 3
1.1. Create a new Go file, for example, test.go, using a text editor or an integrated development environment (IDE).
package main
import "fmt"
func GolangFunction1() {
fmt.Println("Running Golang Function 1")
}
func main() {
fmt.Println("Hello from main function")
GolangFunction1()
}
Now, let's compile the Go file into an executable binary.
cd /path/to/your/directory
Run the following command to compile the Go code into an executable binary named mytestapp:
go build -o mytestapp test.go
The -o flag specifies the output file name (mytestapp in this case), and test.go is the source file.
Now, we'll create a Python script that will call the Go function.
Create a new Python file, for example, test.py, using a text editor or an integrated development environment (IDE).
Add the following Python code to test.py:
import subprocess
def run_go_function(function_name):
try:
# Execute the Go binary and capture its output
result = subprocess.run(['./mytestapp', function_name], stderr=subprocess.STDOUT, text=True)
# Print the output
print(result)
except subprocess.CalledProcessError as e:
# Handle any errors that occur during execution
print(f"Error: {e}")
if __name__ == "__main__":
# Call your Go functions here
run_go_function("GolangFunction1")
Now, let's run the Python script to call the Go function.
Open a terminal and navigate to the directory containing test.py and the mytestapp binary.
Run the Python script using the following command:
python3 test.py
The Python script will execute GolangFunction1 from the Go binary and print its output.
If you encounter permission issues, ensure that the Go binary (mytestapp) has execute permission. You can use the chmod command to set the execute permission:
chmod +x mytestapp
If you encounter issues with function execution, double-check the function name in both your Go code and Python script to ensure they match.
If you need to call a different Go function, modify the Python script to pass the appropriate function name as an argument to the Go binary.
That's it! You have successfully run a Go function from a Python script.