go调用本地python代码

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)

可以看到结果:
在这里插入图片描述

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值