2

I'm trying to call a C function that expects a C string (char*) from go. I know about the C.CString function documented in the cgo documentation but as the function I'm calling will already make a copy, I'm trying to avoid the one Cstring makes.

Right now, I'm doing this, s being a go string

 var cs *C.char = (*C.char)( unsafe.Pointer(& []byte(s) [0]))

But I get the feeling that the []bytes(s) is making its own copy. Is it possible to just get the char* ?

ain
  • 22,394
  • 3
  • 54
  • 74
Romain Francois
  • 17,432
  • 3
  • 51
  • 77

1 Answers1

4

If you're doing this enough times that performance is a concern, it would really be advisable to keep the data in a slice to begin with.

If you really want to access to the address of the string, you can use the unsafe package to convert it into a struct matching the string header. Using the reflect.StringHeader type:

p := unsafe.Pointer((*(*reflect.StringHeader)(unsafe.Pointer(&s))).Data)

Or using a slice as a proxy, since they both put the data pointer and length integers in the same field locations

p := unsafe.Pointer(&(*(*[]byte)(unsafe.Pointer(&s)))[0])

Or because the data pointer is first, you could use a uintptr alone

p := unsafe.Pointer(*(*uintptr)(unsafe.Pointer(&s)))

https://play.golang.org/p/ps1Py7Ax6QK

None of these ways are guaranteed to work in all cases, or in future versions of Go, and none of the options are going to guarantee a null terminated string.

The best, supported option is to create a shim in the cgo preamble to accept the go string, and convert it to a *char. CGO provides access to the following function to do this:

const char *_GoStringPtr(_GoString_ s);

See the Go references to C section in the documentation.

JimB
  • 104,193
  • 13
  • 262
  • 255
  • 3
    Not sure if this applies, but [the release notes of 1.10](https://golang.org/doc/go1.10#cgo) mention the following: «Cgo now supports direct access to Go string values from C. Functions in the C preamble may use the type `_GoString_` to accept a Go string as an argument. C code may call `_GoStringLen` and `_GoStringPtr` for direct access to the contents of the string. A value of type `_GoString_` may be passed in a call to an exported Go function that takes an argument of Go type string.» – kostix Mar 23 '18 at 16:27
  • 1
    @kostix: yes, I almost forgot that! This directly answers the question to convert the data to a `*char`, but inserting a shim to handle the GoString type would be optimal – JimB Mar 23 '18 at 16:31
  • Thanks. To give a bit of a background, this is part of making a bridge between R and go, so sometimes I need to find ways to move go string slices into R character vectors. – Romain Francois Mar 23 '18 at 18:15
  • Thanks @kostix, I'd accept that as an answer if you would write it. – Romain Francois Mar 23 '18 at 18:16
  • 1
    @RomainFrancois thanks, I'm fine with the updated answer by JimB :-) – kostix Mar 23 '18 at 18:29