Package: goinpy
goinpy - Golang In PythonThis 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, seeissue #6. You can use this package for simple examples, but for advanced stuff, try something else (for example, Pythonrequestswith Golang localhost"net/http"listener).Source code is ingoinpy/goinpy.py.InstallationYou can install this package from PyPi withpip install goinpy;Then in python code, import it withfrom goinpy import *.How to useMost of these examples are represented in theexamples/folder.Basic "Hello World!" exampleGolang function exportLet'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 NAMEcomment, what function we need to export.
Also make surefunc main() {}is exists.Compiling to CAfter we made the Golang file, for example,HelloWorld.go, we need to compile it to C.
We can do it by typinggo build -o HelloWorld.so -buildmode=c-shared HelloWorld.goin terminal.
This command is also represented inexample/golangCode.go.If all is okay, we should now see three different files:HelloWorld.go,HelloWorld.so,HelloWorld.h..sois the compiled file that we need.Call Golang function from PythonLet's createHelloWorld.py.from goinpy import *

golangLib = load_go_lib('HelloWorld.so')

golangLib.TestFunc()Here we just imported all fromgoinpypackage, loaded compiled C library intogolangLib, and calledTestFuncfunction with it.After runningpython HelloWorld.pyin a terminal, the output should beHello World!as expected.Advanced examples with different typesInteger examplegolangCode.go://export TestInt
func TestInt(x, y int) int {
    return x + y
}pythonCode.py:setup_go_func(golangLib.TestInt, [intGo, intGo], intGo)
input_1 = intGo(5)
input_2 = intGo(10)
output_result = golangLib.TestInt(input_1, input_2)  # 15Here 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 examplegolangCode.go://export TestFloat
func TestFloat(x float64) float64 {
    return x / 2
}pythonCode.py:setup_go_func(golangLib.TestFloat, [floatGo], floatGo)
input_data = floatGo(12.2)
output_result = golangLib.TestFloat(input_data)  # 6.1Here we met 1 new function:floatGo(float)- convert pythonfloatto golangfloat64.String examplegolangCode.go://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.charfor in and out.
You can convert between this and normal string by usingC.GoString(char)andC.CString(string).pythonCode.py: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 examplegolangCode.go://export TestSlice
func TestSlice(x []int) []int {
    x[0] = 666
    return x
}pythonCode.py: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 examplegolangCode.go://export TestBool
func TestBool(x bool) bool {
    return !(x)
}pythonCode.py:setup_go_func(golangLib.TestBool, [boolGo], boolGo)
input_data = False
output_result = golangLib.TestBool(input_data)  # TrueHere we met 1 new function:boolGo- golangbool. No need in converting python bool to golang bool;NotesIf 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, seeissue #6.OtherCHANGELOGLICENSE