title: Returning CGo String from Go function to Python program
body: <p>So i'm trying to return a string result from a go function to a python string. So far I am returning a <code>*C.char</code> 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.</p>
  
  <p>Go part:</p>
  
  <pre><code>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() {}
  </code></pre>
  
  <p>Python part:</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>output:</p>
  
  <pre><code>Go recieved: PYTHON MESSAGE
  Python reads: c_char_p(4607968)
  </code></pre>
  
  <p>I have tried using <code>result.value</code> and <code>c_string_from(result)</code>, 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. </p>