Convert Go []byte to a C *char

前端 未结 2 705
粉色の甜心
粉色の甜心 2021-02-02 17:07

I have a byte.Buffer that I pack with data using the binary.Write() function. I then need to send this byte array to a C function. Using Go 1.6 I have not been successful at fig

2条回答
  •  忘掉有多难
    2021-02-02 17:31

    Your program crashes because rules of passing pointers into C changed in go1.6 (see https://tip.golang.org/doc/go1.6#cgo for details).

    I don't know why your program crashes, so I have created Go issue https://github.com/golang/go/issues/14546.

    But regardless of answer on the issue, I wouldn't use internal bits of bytes.Buffer (as you do) to pass into cgo directly. The bytes.Buffer implementation can change in the future, and you program will start breaking mysteriously. I would just copy data you need into whatever structure is appropriate and use that to pass into cgo.

提交回复
热议问题