I have a little question about C and Go interaction.
Imaginge that we have a C function which returns pointer to some buffer.
> char *foo() // Please, forget about size, it is not importent for us now.
So, the problem (in Go code) is to copy this buffer's content to Go-array.
> buf := C.foo()
> ???
I can't find the way to do it. Any ideas?
--
Best regards, Viacheslav Chumushuk
email/jabber: vo...@root.ua
Ukraine, Khmelnitsky
I have a little question about C and Go interaction.
Imaginge that we have a C function which returns pointer to some buffer.> char *foo() // Please, forget about size, it is not importent for us now.
So, the problem (in Go code) is to copy this buffer's content to Go-array.
> buf := C.foo()
> ???I can't find the way to do it. Any ideas?
On Fri, Jun 10, 2011 at 07:24:03AM -0700, Jan Mercl <jan....@nic.cz> wrote:
> If the C character buffer is a C string, i.e. it's null terminated, then it
> should be possible to use C.GoString(). Example e.g. here:
> http://golang.org/misc/cgo/stdio/file.go
--
No, it is simple raw bytes buffer.
And I'd like to convert it to []int8 array.
--
Don't use unsafe unless you really understand the implications,
otherwise you'll see strange crashes at weird locations. Instead, use
GoStringN:
s := C.GoStringN(foo, n)
--
Gustavo Niemeyer
http://niemeyer.net
http://niemeyer.net/blog
http://niemeyer.net/twitter
--
You can do []byte(s)
These documents are very helpful for figuring the basics out:
http://golang.org/doc/effective_go.html
http://golang.org/doc/go_tutorial.html
--