I'm trying to instantiate a C struct inside my Go code. The struct is defined like this, (in an external library that I cannot modify):
typedef struct {
char field1[256];
} S1
In go, I did this:
func myfunc(mystr string){
// We need to convert mystr from string to char array
cStr := C.CString(mystr)
defer C.free(unsafe.Pointer(cStr)
// Should work now
s1 := &C.S1{field1: cStr}
// Do something with s1...
}
But it doesn't compile because:
cannot use cStr (type *C.char) as type [256]C.char in field value
I've tried forcing ([256]C.char)(cStr) but it obviously doesn't work either.
Is there a way to achieve what I'm trying to do?
Unfortunately there aren't any convenience methods for handling
[size]C.char
as a string in Go (I think I saw a proposal to add this at one point though...)In my code instead of handling it directly, I opted to manually write the string into the struct when needed with some like
And the way I used to handle it, which is much less safe is
The simplest solution is to change your struct's field definition to a char-pointer which is pretty standard for strings in C:
The more complex solution would be [1]:
[1] Code untested, cannot compile on this workstation.