* Andy Balholm <
andyb...@gmail.com> [2013-01-24 20:33:42 -0800]:
* Kevin Gillette <
extempor...@gmail.com> [2013-01-24 19:27:24 -0800]:
> If the length of the C array is invariant, you can use a go array pointer
> value, such as *[30]uint32 and assign the pointer to the C data via
> unsafe.Pointer. If it's variable length, you can construct a []uint32 slice
> header using unsafe and reflect.
* minux <
minu...@gmail.com> [2013-01-25 18:21:54 +0800]:
> or just use a ridiculously large array length, for example, 1<<32, and then
> slice it.
Thanks guys. I couldn't get slice conversion working (I was following
[1]). In the end I used GoBytes() and binary.Read(); any improvements on
this solution?
> // OidToStr converts an oid from C array of guint32's to a Go string
> func OidToStr(oid *_Ctype_guint32, oid_len _Ctype_gsize) (result string) {
> size := int(unsafe.Sizeof(oid))
> length := int(oid_len)
> gbytes := C.GoBytes(unsafe.Pointer(oid), (_Ctype_int)(size*length))
>
> buf := bytes.NewBuffer(gbytes)
> for i := 0; i < length; i++ {
> var out uint32
> if err := binary.Read(buf, binary.LittleEndian, &out); err == nil {
> result = result + fmt.Sprintf(".%d", out)
> } else {
> return "<error converting oid>"
> }
> }
> return result[1:]
> }
[1]
https://code.google.com/p/go-wiki/wiki/cgo, "Turning C arrays into
Go slices"