cgo

How do I convert a Go array of strings to a C array of strings?

送分小仙女□ 提交于 2020-12-29 04:11:36
问题 I am using cgo in a project, and I want to export a function for use. Here's an example of what I want to achieve: package csplit import ( "C" "strings" ) //export Split /* The Split function takes two C strings, the second of which represents a substring to split on, and returns an array of strings. Example: Split("1,2", ",") // gives ["1", "2"] */ func Split(original *C.char, split *C.char) []*C.char { goResult := strings.Split(C.GoString(original), C.GoString(split)) cResult := make([]*C

passing function pointer to the C code using cgo

不想你离开。 提交于 2020-12-29 04:02:36
问题 Starting from Go v1.6 cgo changed the rules of passing pointers to the C code golang/go#12416. The example of invoking a dynamic Go callback from C code from the wiki doesn't work anymore. package main import ( "fmt" "unsafe" ) /* extern void go_callback_int(void* foo, int p1); // normally you will have to define function or variables // in another separate C file to avoid the multiple definition // errors, however, using "static inline" is a nice workaround // for simple functions like this

passing function pointer to the C code using cgo

我与影子孤独终老i 提交于 2020-12-29 04:01:57
问题 Starting from Go v1.6 cgo changed the rules of passing pointers to the C code golang/go#12416. The example of invoking a dynamic Go callback from C code from the wiki doesn't work anymore. package main import ( "fmt" "unsafe" ) /* extern void go_callback_int(void* foo, int p1); // normally you will have to define function or variables // in another separate C file to avoid the multiple definition // errors, however, using "static inline" is a nice workaround // for simple functions like this

passing function pointer to the C code using cgo

試著忘記壹切 提交于 2020-12-29 04:01:06
问题 Starting from Go v1.6 cgo changed the rules of passing pointers to the C code golang/go#12416. The example of invoking a dynamic Go callback from C code from the wiki doesn't work anymore. package main import ( "fmt" "unsafe" ) /* extern void go_callback_int(void* foo, int p1); // normally you will have to define function or variables // in another separate C file to avoid the multiple definition // errors, however, using "static inline" is a nice workaround // for simple functions like this

Go/CGo - how do you use a C array passed as a pointer

眉间皱痕 提交于 2020-12-05 07:05:25
问题 I'm posting this as a question/answer, as it took me a while to work out, and I wouldn't mind some feedback on my solution. In Go/CGo, how do you work with a C array passed as a pointer? For example, with this C struct: struct _GNetSnmpVarBind { guint32 *oid; /* name of the variable */ gsize oid_len; /* length of the name */ ... and other fields }; I want to convert oid field to a Go string, how would I work with the guint32* pointer? 回答1: You could convert the C array into a Go slice using a

How to access C bitfield in Go

可紊 提交于 2020-08-27 12:45:34
问题 I have a struct like so: typedef struct st_MASK_SETTINGS { uint32_t foo : 1; uint32_t bar : 7; } MASK_SETTINGS Now through cgo I would like to access foo - but cannot find any documentation how to do so. Naive v := ms.foo complains has no field or method . 回答1: Well you won't like this answer, but there is no portable way to do this because bitfield packing is "implementation-defined" in both C and C++, and bitfield support in Go seems fairly crappy (perhaps due to #1). First of all, the

Cgo: can't find way to use callbacks with const char* argument

那年仲夏 提交于 2020-08-07 10:34:12
问题 I'm use C library from Go using Cgo and all good except callbacks. Library have callback setter, which takes pointer to callback func. Callback func itself written in go and exported using Cgo syntax. Problem: I can make and export function with char * argument, but can't with const char * . Code to illustrate: test.go : package main /* typedef void (*cb_func)(const char *, int); void callback(cb_func); void myFunc(const char *, int); */ import "C" import ( "fmt" "unsafe" ) //export myFunc

Cgo: can't find way to use callbacks with const char* argument

青春壹個敷衍的年華 提交于 2020-08-07 10:33:00
问题 I'm use C library from Go using Cgo and all good except callbacks. Library have callback setter, which takes pointer to callback func. Callback func itself written in go and exported using Cgo syntax. Problem: I can make and export function with char * argument, but can't with const char * . Code to illustrate: test.go : package main /* typedef void (*cb_func)(const char *, int); void callback(cb_func); void myFunc(const char *, int); */ import "C" import ( "fmt" "unsafe" ) //export myFunc