sbinet / go-python

naive go bindings to the CPython2 C-API
Other
1.53k stars 139 forks source link

How to call a function of Go from python #105

Closed khaitranvan96kt closed 4 years ago

khaitranvan96kt commented 4 years ago

In my problem, I'm trying to use go to call a function of python and from that function I want to call another function of golang then return the result. Please help me

// hello.py
def hello(fun_go):
    """
    Print hello world for fun and profit.
    """
    print("hello, world! Python")
    if fun_go is None:
        print ('fun_go is None')
    else:
        fun_go()

// test.go

func FunGo() {
    fmt.Println("Hello Go")
}

func main() {
    python.Initialize()
    defer python.Finalize()

    fooModule := python.PyImport_ImportModule("function")
    if fooModule == nil {
        panic("Error importing module")
    }

    helloFunc := fooModule.GetAttrString("hello")
    if helloFunc == nil {
        panic("Error importing function")
    }

    // how can i set FunGo as a parameter when calling python hello
    helloFunc.Call(python.PyTuple_New(0), python.PyDict_New())
}
sbinet commented 4 years ago

AFAIR, that's not possible with sbinet/go-python.

khaitranvan96kt commented 4 years ago

AFAIR, that's not possible with sbinet/go-python.

Thank you for letting me know, Do you know of any other ways that can help me solve this problem?

sbinet commented 4 years ago

perhaps https://gopy.qur.me/extensions/