Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unable to use unsafe.Pointer as pointer to C struct in Go 1.8 #19181

Closed
ghost opened this issue Feb 19, 2017 · 2 comments
Closed

unable to use unsafe.Pointer as pointer to C struct in Go 1.8 #19181

ghost opened this issue Feb 19, 2017 · 2 comments

Comments

@ghost
Copy link

ghost commented Feb 19, 2017

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)?

GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/brandonhsiao/gocode"
GORACE=""
GOROOT="/usr/local/Cellar/go/1.8/libexec"
GOTOOLDIR="/usr/local/Cellar/go/1.8/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/_v/1ll1sr8d3ml0j1gxhbkfmg580000gp/T/go-build439503373=/tmp/go-build -gno-record-gcc-switches -fno-common"
CXX="clang++"
CGO_ENABLED="1"
PKG_CONFIG="pkg-config"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"

What did you do?

package main

/*
#include <stdio.h>

typedef struct {
  int foo;
} CStruct;

void test(CStruct* p) {
  printf("%d", p->foo);
}
*/
import "C"
import "unsafe"

type CStruct _Ctype_CStruct

func main() {
  var x CStruct
  x.foo = 42

  C.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
@ghost 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
@minux
Copy link
Member

minux commented Feb 19, 2017 via email

@ghost
Copy link
Author

ghost commented 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.

@ghost ghost closed this as completed Feb 19, 2017
@golang golang locked and limited conversation to collaborators Feb 19, 2018
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants