Re: [go-nuts] CGO: Go string -> void*/length

2016-10-31 Thread Pietro Gagliardi
I was going by the example at the bottom of the OP. Use whatever type is expected by the C API, of course. > On Oct 31, 2016, at 4:41 PM, Florian Weimer wrote: > > * Pietro Gagliardi: > >> Just pass the null-terminated string and use C.int(len(goString)) as >> the length. The length of a Go st

Re: [go-nuts] CGO: Go string -> void*/length

2016-10-31 Thread Florian Weimer
* Pietro Gagliardi: > Just pass the null-terminated string and use C.int(len(goString)) as > the length. The length of a Go string is already in bytes and does not > include the terminating null (since Go has none), and I assume > C.CString() produces the same byte sequence without encoding > conv

Re: [go-nuts] CGO: Go string -> void*/length

2016-10-31 Thread James Bardin
C.CBytes does essentially this, but you either need to start with a []byte, or the string conversion could be a second copy. I don't see a need for a separate version CString without the null when you can just pass the length-1. In the rare case that the extra byte makes a difference, you coul

Re: [go-nuts] CGO: Go string -> void*/length

2016-10-31 Thread Peter Mogensen
On 2016-10-31 15:39, Pietro Gagliardi wrote: Just pass the null-terminated string and use C.int(len(goString)) as the length. Yes... I mentioned that as a somewhat solution. ... it still allocates 1 byte more than needed. /Peter -- You received this message because you are subscribed to t

Re: [go-nuts] CGO: Go string -> void*/length

2016-10-31 Thread Pietro Gagliardi
Just pass the null-terminated string and use C.int(len(goString)) as the length. The length of a Go string is already in bytes and does not include the terminating null (since Go has none), and I assume C.CString() produces the same byte sequence without encoding conversions. > On Oct 31, 2016,