You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
package main
/*#include <stdio.h>typedef struct { int foo;} CStruct;void test(CStruct* p) { printf("%d", p->foo);}*/import"C"import"unsafe"typeCStruct_Ctype_CStructfuncmain() {
varxCStructx.foo=42C.test(unsafe.Pointer(&x))
}
$ go build main.go && ./main
What did you expect to see?
A working compilation, and 42 printed to the screen. This worked in Go 1.7, and the Go docs state
- A Pointer can be converted to a pointer value of any type.
What did you see instead?
$ go build main.go
# command-line-arguments
./main.go:23: cannot use unsafe.Pointer(&x) (type unsafe.Pointer) as type *C.struct___0 in argument to _Cfunc_test
The text was updated successfully, but these errors were encountered:
ghost
changed the title
Unable to use unsafe.Pointer as pointer to C struct in Go 1.8
unable to use unsafe.Pointer as pointer to C struct in Go 1.8
Feb 19, 2017
Was the fact that it worked in 1.7 a bug? Also, this doesn't work either:
C.test((*CStruct)(unsafe.Pointer(&x)))
as it prints out
$ go build main.go
# command-line-arguments
./main.go:23: cannot use (*CStruct)(unsafe.Pointer(&x)) (type *CStruct) as type *C.struct___0 in argument to _Cfunc_test
Is there a way to do this?
Edit: It looks like using C.struct___0 and *C.struct___0 works. Does this mean we're forced to refer to C structs by index? How are they indexed?
Edit 2: Wow, it looks like you can just directly use C.CStruct! Apparently this is outdated.
What version of Go are you using (
go version
)?go version go1.8 darwin/amd64
What operating system and processor architecture are you using (
go env
)?What did you do?
What did you expect to see?
A working compilation, and
42
printed to the screen. This worked in Go 1.7, and the Go docs stateWhat did you see instead?
The text was updated successfully, but these errors were encountered: