Chris
Chris

Reputation: 685

create golang bindings for a python module

I want to write golang bindings for an existing (third party) Python module. The purpose is that I want to use the API that the Python module provides in Golang.

I already found golang bindings for Python's C API (go-python3 for py3 and go-python for py2), but I still haven't figured out how to translate a relatively complex Python module into Golang (i.e. how to deal with type safety in go for unsafe inputs and returns in python, etc).

What would be a good approach? Are there any pre-existing tools in that space? Are there any good examples for Golang bindings for Python code? (I couldn't find many tbh).

Upvotes: 14

Views: 10874

Answers (1)

VonC
VonC

Reputation: 1326766

I want to use the API that the Python module provides in Golang.

Calling Python from Go is detailed recently in "Python and Go : Part I - gRPC" by Miki Tebeka.
You can see an example in ardanlabs/python-go/grpc

But, as shown in their next two articles, you can also:

  • compiled Go code to a shared library and used it from the Python interactive shell.
  • use a Python module that hides the low level details of working with a shared library and then package this code as a Python package.

https://www.ardanlabs.com/images/goinggo/124_figure1.png

Full example: ardanlabs/python-go/pyext.

Upvotes: 13

Related Questions