Extending Python 3 in Go

Last updated 1 year ago by Anthony Shaw


Extending Python has been a core feature of the platform for decades, the Python runtime provides a “C API”, which is a set of headers and core types for writing extensions in C and compiling them into Python modules.

But, do you really have to write extensions to Python in C? Why can’t we use something a tad more modern, like Go.

Background research — hasn’t this been done before?

I found a number of old articles written in 2015 either for Python 2 or for Go 1.5. Go has changed a lot since then so finding something up to date and well maintained was hard.

This article was for Go 1.5, and uses a mixture of C macros and Go. Also, a big limitation of cgo instead of the native Go functions is that it doesn’t support variadics (i.e. *args), which will be a big issue in most Python modules.

gopy, a tool for automatically creating modules from Go packages. It seemed from a glance like it was going to cause issues because it required a particular flag at compile-time which is not recommended in production. Also, it doesn’t support Python 3.

Read full Article