Skip to content

Instantly share code, notes, and snippets.

View SeanTolstoyevski's full-sized avatar

Emre SeanTolstoyevski

  • ICI Tech Teknoloji
  • Eskisehir, Turkiye
  • 12:56 (UTC +03:00)
View GitHub Profile
@helinwang
helinwang / gostring.md
Last active January 3, 2023 07:50
Call go (or c) code from python with string as argument and string as return value

go code (foo.go) compiled into a shared library and a c header (foo.h) is generated.

For calling go from c, please see here

Arguments

The code below shows two ways of passing string parameter to go function:

  1. Using GoString structure as argument, without making a copy in go code: no conversion to go string needed.
  2. Using c_char_p as argument, making a copy in go code when converting to go string.

When using the first method without the copy, I don't know how python will do the memory management with the pointer passed into go. So the second method is preferred.