Use Golang functions inside Python code
Project description
goinpy - Golang In Python
This is a python package, which is made to use Golang functions inside Python code more easily.
Embedding Golang functions in python can be very handy, for example, if you want to move some big computations from slow Python to faster Golang and immediately get a result back.
For now (and probably ever), Golang and Python full function embedding can't be implemented due languages nature, see issue #6. You can use this package for simple examples, but for advanced stuff, try something else (for example, Python requests with Golang localhost "net/http" listener).
Source code is in goinpy/goinpy.py.
Installation
You can install this package from PyPi with pip install goinpy;
Then in python code, import it with from goinpy import *.
How to use
Most of these examples are represented in the examples/ folder.
Basic "Hello World!" example
Golang function export
Let's start with exporting a simple Golang "Hello World!" function.
package main
import (
"C"
"fmt"
)
//export TestFunc
func TestFunc() {
fmt.Println("Hello World!")
}
func main() {
}
To export functions, we need to import "C" library, and specify by //export NAME comment, what function we need to export.
Also make sure func main() {} is exists.
Compiling to C
After we made the Golang file, for example, HelloWorld.go, we need to compile it to C.
We can do it by typing go build -o HelloWorld.so -buildmode=c-shared HelloWorld.go in terminal.
This command is also represented in example/golangCode.go.
If all is okay, we should now see three different files: HelloWorld.go, HelloWorld.so, HelloWorld.h.
.so is the compiled file that we need.
Call Golang function from Python
Let's create HelloWorld.py.
from goinpy import *
golangLib = load_go_lib('HelloWorld.so')
golangLib.TestFunc()
Here we just imported all from goinpy package, loaded compiled C library into golangLib, and called TestFunc function with it.
After running python HelloWorld.py in a terminal, the output should be Hello World! as expected.
Advanced examples with different types
Integer example
//export TestInt
func TestInt(x, y int) int {
return x + y
}
setup_go_func(golangLib.TestInt, [intGo, intGo], intGo)
input_1 = intGo(5)
input_2 = intGo(10)
output_result = golangLib.TestInt(input_1, input_2) # 15
Here we met 2 new functions:
intGo(int)- convert pythonintto golangint. You can convert it back bysome_int.value;setup_go_func(func, arg_types=None, res_type=None)- if Golang function is taking or returning some data, we need to setup this function. Firstfuncarg is the function we are trying to setup. Secondarg_typesarg is a list for types this function is waiting. Thirdres_typearg is a type it's returning. Also it returnsfuncarg back for cases when you want to setup function and assign it to variable in one line.
Float example
//export TestFloat
func TestFloat(x float64) float64 {
return x / 2
}
setup_go_func(golangLib.TestFloat, [floatGo], floatGo)
input_data = floatGo(12.2)
output_result = golangLib.TestFloat(input_data) # 6.1
Here we met 1 new function:
floatGo(float)- convert pythonfloatto golangfloat64.
String example
//export TestString
func TestString(x *C.char) *C.char {
str := C.GoString(x)
newStringC := C.CString("Hello, " + str)
return newStringC
}
Note that for strings, we need to use *C.char for in and out.
You can convert between this and normal string by using C.GoString(char) and C.CString(string).
setup_go_func(golangLib.TestString, [stringGo], stringGo)
input_data = str_to_go('World')
output_result = str_to_py(golangLib.TestString(input_data)) # "Hello, World!"
Here we met 3 new functions:
stringGo- golangstring;str_to_go(str)- convert pythonstrto golangstring;str_to_py(string)- convert golangstringto pythonstr.
Slice example
//export TestSlice
func TestSlice(x []int) []int {
x[0] = 666
return x
}
setup_go_func(golangLib.TestSlice, [intGoSlice], intGoSlice)
input_list = [intGo(123), intGo(456)]
input_data = list_to_slice(input_list, intGo)
output_result = slice_to_list(golangLib.TestSlice(input_data)) # [666, 456]
Here we met 3 new functions:
intGoSlice- golang[]int slice. There is alsofloatGoSlice,stringGoSliceandboolGoSlice;list_to_slice(list, data_type: None)- convert pythonlistto golangslice. First arg is actual list we are converting. Second additionaldata_typearg is what type this slice is storing (NOTE THAT SLICE CAN'T STORE DIFFERENT FILE TYPES AT ONCE).slice_to_list(slc)- convert golangsliceto pythonlist.
Bool example
//export TestBool
func TestBool(x bool) bool {
return !(x)
}
setup_go_func(golangLib.TestBool, [boolGo], boolGo)
input_data = False
output_result = golangLib.TestBool(input_data) # True
Here we met 1 new function:
boolGo- golangbool. No need in converting python bool to golang bool;
Notes
- If multiple
.solibraries is imported, make sure they are compiled under different names; - Generated
.sofile will only work on the same system. For example, if it's generated on Windows (like in this repo), it will not work on Linux or Mac; - You can't create slices inside slices (issue #3);
- Supported types are: int, float64, string, bool and slice containing any of previous 4 types;
- Golang function can't return more than 1 variable to python.
- In case of any weird error, see issue #6.
Other
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file goinpy-0.4.tar.gz.
File metadata
- Download URL: goinpy-0.4.tar.gz
- Upload date:
- Size: 5.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
936585eb503cc6d8e30e4b794924594fa3be92ad3fdac8a3e86ca8cf468dc030
|
|
| MD5 |
f188a1b07d1484ce0089527c5a949f18
|
|
| BLAKE2b-256 |
c6deeaa0938bb5fdf64b18316ef6d2a3e0406422bfef319d9796229d9f243785
|
File details
Details for the file goinpy-0.4-py3-none-any.whl.
File metadata
- Download URL: goinpy-0.4-py3-none-any.whl
- Upload date:
- Size: 5.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3f6099bcbc78f30961bcbcf41bbd9588d1c32239750c693829faa972101282d0
|
|
| MD5 |
339e7adc277497f0f26567699f7169cf
|
|
| BLAKE2b-256 |
7b56ae4b197fd51551c9ed340b5835ce5edc5ea8ea55361dd8ae124a7f109a11
|