go调用本地python代码

1、mac环境下测试
目录结构:

go代码:(windows没有python3命令,windows的话改成python即可)

func main() {
	test()
}
func test()  {
	cmd :=exec.Command("python3","./a.py")
	data,_ :=cmd.Output()
	fmt.Println(string(data))
}

a.py文件,python直接打印一条数据即可

def test():
    print(111)

test()

可以看到结果:

2、go传参数调用python代码
同样的目录结构
go:

func main() {
	test()
}
func test()  {
	cmd :=exec.Command("python3","./a.py","hello","world")
	data,_ :=cmd.Output()
	fmt.Println(string(data))
}

python:

import sys

str1 = sys.argv[1]
str2 = sys.argv[2]


def test(str1, str2):
    print(str1+","+str2)

test(str1,str2)

可以看到结果: