Where are cgo C.* helper functions implemented and is there godoc for them?

328 views
Skip to first unread message

Tim K

unread,
Dec 16, 2015, 1:36:00 PM12/16/15
to golang-nuts

I'm curious to look at the source for C.GoBytes() but not sure where to look or how it all works.

Also, is there more documentation/godoc for the C.* helper functions beyond this page? For example it was not clear to me whether C.GoBytes() copies data or just creates a slice backed by the C bytes. It's probably clear though for someone with more C and Go experience than me :-)

Thanks.

Tim

Ian Lance Taylor

unread,
Dec 16, 2015, 1:59:04 PM12/16/15
to Tim K, golang-nuts
On Wed, Dec 16, 2015 at 10:36 AM, Tim K <tim....@gmail.com> wrote:
>
> I'm curious to look at the source for C.GoBytes() but not sure where to look
> or how it all works.

The source code is in cmd/cgo/out.go:

//go:linkname _cgo_runtime_gobytes runtime.gobytes
func _cgo_runtime_gobytes(unsafe.Pointer, int) []byte

func _Cfunc_GoBytes(p unsafe.Pointer, l _Ctype_int) []byte {
return _cgo_runtime_gobytes(p, int(l))
}

The call to _cgo_runtime_gobytes will call runtime.gobytes, which is
defined in runtime/string.go.


> Also, is there more documentation/godoc for the C.* helper functions beyond
> this page? For example it was not clear to me whether C.GoBytes() copies
> data or just creates a slice backed by the C bytes. It's probably clear
> though for someone with more C and Go experience than me :-)

The only docs are https://golang.org/cmd/cgo. That page does say "by
making copies of the data."

Ian

Tim K

unread,
Dec 16, 2015, 2:03:49 PM12/16/15
to golang-nuts, tim....@gmail.com

Thanks. And I read that page so many times and still missed the copy part :-( My eyes glanced right through the text straight to the code block. My fault, sorry.
 
Tim
Reply all
Reply to author
Forward
0 new messages