cgo

cgo: How to pass struct array from c to go

╄→尐↘猪︶ㄣ 提交于 2019-11-28 05:18:34
问题 The C part: struct Person {...} struct Person * get_team(int * n) The Go part: n := C.int(0) var team *C.struct_Person = C.get_team(&n) defer C.free(unsafe.Pointer(team)) I can get the first element of the array in this way. But how to get the whole array with n elements? and how to free them safely? 回答1: First, even though you’re using Go, when you add cgo there is no longer any "safe". It's up to you to determine when and how you free the memory, just as if you were programming in C. The

How to pass pointer to slice to C function in go

心已入冬 提交于 2019-11-28 04:34:05
问题 Background: using cgo to call C functions from Golang. I want to use a C function which has this signature: int f(int *count, char ***strs) . It will modify the data of count and strs , which is the reason why it uses pointer to them. The value of count is the length of strs ; strs is an array of string; the return value is simply an (boolean) indicator which states whether there is an error or not. In golang, I can successfully pass and modify count by using C.f((*C.int)(&count)) ; pass [

How do I copy a Go string to a C char * via CGO in golang?

若如初见. 提交于 2019-11-28 04:02:11
问题 I want to copy a Go string into a char * via CGO. Am I allowed to do this something like this? func copy_string(cstr *C.char) { str := "foo" C.GoString(cstr) = str } 回答1: According to the cgo documentation you need to use the C.CString function to convert a Go string to a C string: cstr = C.CString(str) Be aware that C.CString function allocates the memory for you, but won't release it, so it is your responsability to freed the memory with a call like: C.free(unsafe.Pointer(cstr)) 来源: https:/

What does (*[1 << 30]C.YourType) do exactly in CGo?

泪湿孤枕 提交于 2019-11-28 02:05:11
问题 In the Golang wiki, under "Turning C arrays into Go slices", there is a block of code that demonstrates how to create a Go slice backed by a C array. import "C" import "unsafe" ... var theCArray *C.YourType = C.getTheArray() length := C.getTheArrayLength() slice := (*[1 << 30]C.YourType)(unsafe.Pointer(theCArray))[:length:length] Can anyone explain exactly what (*[1 << 30]C.YourType) does? How does it turn an unsafe.Pointer into a Go slice? 回答1: *[1 << 30]C.YourType doesn't do anything itself

How do you statically link a c library in go using cgo?

两盒软妹~` 提交于 2019-11-27 06:40:27
So there's a bunch of stuff on the group that suggests you can do this in go (although not on the cgo documentation): package bridge import "fmt" // #cgo CFLAGS: -I/Users/doug/projects/c/go-bridge/include // #cgo LDFLAGS: /Users/doug/projects/c/go-bridge/build/libgb.a // #include <junk.h> import "C" func Run() { fmt.Printf("Invoking c library...\n") C.x(10) fmt.Printf("Done\n") } However, it doesn't seem to work: /var/folders/.../bridge.a(bridge.cgo2.o)(__TEXT/__text): x: not defined This seems to work fine using a dynamic library, and inspecting the generated files, it actually has the symbol