使用Gopy进行转换Go代码到python包调用

https://github.com/go-python/gopy?tab=readme-ov-file Go代码封装接口到Python

依赖

$ python3 -m pip install pybindgen
$ go install golang.org/x/tools/cmd/goimports@latest
$ go install github.com/go-python/gopy@latest

编译包

python需要在环境变量PATH里,用于寻找编译依赖的CFLAG

  • 构建直接调用代码
go get github.com/go-python/gopy/_examples/hi
gopy build -output=out -vm=python github.com/go-python/gopy/_examples/hi

output: 输出文件夹
mod下需要完整包路径名字

测试代码

from out import hi
hi.Hello("you")
print(dir(hi))
  • 构建python wheel包
gopy pkg -output=out1  -vm=python github.com/go-python/gopy/_examples/hi;

cd out1;  # 生成了python包开发结构
python setup.py bdist_wheel
Logo

Agent 垂直技术社区,欢迎活跃、内容共建。

更多推荐