So i'm trying to return a string result from a go function to a python string. So far I am returning a *C.char
from go and getting this into python, I am just having trouble de-referencing of the pointer, since this is something I am not familiar with in python.
Go part:
package main
import "C"
import (
"fmt"
)
//export HelloGo
func HelloGo(a string) *C.char {
fmt.Println("Go recieved:", a)
return C.CString(fmt.Sprintf("Hello from Go, %s!", a))
}
func main() {}
Python part:
import ctypes as c
text = b"PYTHON MESSAGE"
lib = c.cdll.LoadLibrary("./hey.so")
class GoString(c.Structure):
_fields_ = [("p", c.c_char_p), ("n", c.c_longlong)]
lib.HelloGo.argtypes = [GoString]
gs = GoString(text, len(text))
result = c.c_char_p(lib.HelloGo(gs))
print("Python reads:", result)
output:
Go recieved: PYTHON MESSAGE
Python reads: c_char_p(4607968)
I have tried using result.value
and c_string_from(result)
, but these give me a segmentation fault. I have had no difficulty getting the result in a C program and understand I can write a Python extension to retrieve this but this seems a bit convoluted.