Re: [go-nuts] Converting a C char[] to []byte of string with cgo

2020-04-30 Thread Dean Schulze
That works. The problem I had earlier with a panic was caused by my C library which was returning null as an error condition. Once the C code was fixed it worked as you said. Thanks. On Thu, Apr 30, 2020 at 2:18 PM Jan Mercl <0xj...@gmail.com> wrote: > On Thu, Apr 30, 2020 at 10:06 PM Dean

Re: [go-nuts] Converting a C char[] to []byte of string with cgo

2020-04-30 Thread Dean Schulze
> > That compiles but it still panics: > > test: bad machine line11=(null), return > panic: runtime error: invalid memory address or nil pointer dereference > [signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x4949f2] > > goroutine 1 [running]: > main.(*LineStruct).MachineName(...) >

Re: [go-nuts] Converting a C char[] to []byte of string with cgo

2020-04-30 Thread Bruno Albuquerque
C.GoString((*C.char)(_name)) On Thu, Apr 30, 2020 at 1:21 PM Dean Schulze wrote: > This expression > > C.GoString(C.char*(_name)) > > gives this error: > > type _Ctype_char is not an expression > > On Thu, Apr 30, 2020 at 2:09 PM Bruno Albuquerque wrote: > >> Not pretty but if you

Re: [go-nuts] Converting a C char[] to []byte of string with cgo

2020-04-30 Thread Dean Schulze
This expression C.GoString(C.char*(_name)) gives this error: type _Ctype_char is not an expression On Thu, Apr 30, 2020 at 2:09 PM Bruno Albuquerque wrote: > Not pretty but if you are sure the array is zero-terminated, you can > simply cast it to the correct type: > >

Re: [go-nuts] Converting a C char[] to []byte of string with cgo

2020-04-30 Thread Jan Mercl
On Thu, Apr 30, 2020 at 10:06 PM Dean Schulze wrote: > > That gives this error (which I've gotten with just about everything I try): > > cannot use _name (type *[25]_Ctype_char) as type *_Ctype_char in > argument to _Cfunc_GoString Oh yeah, should have been probably `C.GoString([0])`. Forgot Go

Re: [go-nuts] Converting a C char[] to []byte of string with cgo

2020-04-30 Thread Bruno Albuquerque
Not pretty but if you are sure the array is zero-terminated, you can simply cast it to the correct type: C.GoString((C.char*)()) (untested) On Thu, Apr 30, 2020 at 1:06 PM Dean Schulze wrote: > That gives this error (which I've gotten with just about everything I try): > > cannot use _name

Re: [go-nuts] Converting a C char[] to []byte of string with cgo

2020-04-30 Thread Dean Schulze
That gives this error (which I've gotten with just about everything I try): cannot use _name (type *[25]_Ctype_char) as type *_Ctype_char in argument to _Cfunc_GoString It seems to be the fixed length of the char[] that gives cgo a problem. On Thursday, April 30, 2020 at 1:46:34 PM UTC-6, Jan

Re: [go-nuts] Converting a C char[] to []byte of string with cgo

2020-04-30 Thread Jan Mercl
On Thu, Apr 30, 2020 at 8:17 PM Dean Schulze wrote: > > I must be missing something pretty simple, but I have a C struct like this: > > typedef struct line > { > char hostname[HOSTNAME_MAX]; > char ip[IP_MAX]; > ... > } > > When I try to do the simple thing > > C.GoString(p.hostname)

[go-nuts] Converting a C char[] to []byte of string with cgo

2020-04-30 Thread Dean Schulze
I must be missing something pretty simple, but I have a C struct like this: typedef struct line { char hostname[HOSTNAME_MAX]; char ip[IP_MAX]; ... } When I try to do the simple thing C.GoString(p.hostname) I get an error like cannot use p.hostname (type [25]_Ctype_char) as type