Do you ever wish Python were faster? go4py
is here to help!
Enjoy the speed and simplicity of Go within Python! You can write bottlenecks of your Python code as CGo functions. The go4py
will automatically generate a Python module from it for you!
Check the performance boost in our benchmarks and get a sense of how much faster it can be compared to basic Python! 🚀
- Install Go
pip install go4py
- Install Go
- Install MSYS2 and then install mingw-toolchain inside it.
- Install
go4py
in a python vitrual exnvironment. - Open a mingw shell and activate the venv inside it (e.g.
source venv/Scripts/activate
) - Add Go to your PATH in the mingw shell (e.g.,
export PATH=$PATH:"/c/Program Files/Go/bin/"
)
To create a Go package inside your Python project:
go4py init <module_name>
This will create a directory with the name <module_name>
and add go.mod
to your project.
You can go to <module_name>/src
directory and add your Go functions there.
To build the Go package and generate bindings code for the Python module:
go4py build <module_name>
You can now import the functions as methods of a python module:
from <module_name> import <function_name>
Please note that we convert the function names' first character to lower case.
There is a Makefile in the created directory with four major steps:
- Parsing the Go code and extracting the signatures of the functions (the
functions.json
file). - Building an object file from Go code (
.a
and.h
files). - Making a C wrapper for the Go functions as a Python native C extension.
- Building a shared library from the generated C code (
__init__.so
file or__init__.pyd
file).
The Makefile is actually quite readable, and you can modify it to your needs go4py
will just run it
This documentation will explain how to use go4py. there is also a examples directory for some examples. But if you need to dive into inner-workings of go4py
take a look at the tests directory and checkout the codes that go4py
generates automaticly from a go function signature.