Golang CGo: converting union field to Go type

go

Solution

cgo exposes a union as a byte array large enough to hold the largest member of the union. In your case that is 64 bits which are 8 bytes, `[8]byte`. As you've demonstrated, the contents of this array hold the contents of the union and using it is a matter of pointer conversion.

However, you can use the address of the array to greatly simplify the process. For a `C._GNetSnmpVarBind` named `data`,

guint32_star := *(**C.guint32)(unsafe.Pointer(&data.value[0]))

I didn't fully understand this the first time I saw it, but it became more clear when I broke it down:

var data C._GNetSnmpVarBind    // The C struct
var union [8]byte = data.value // The union, as eight contiguous bytes of memory

// The first magic. The address of the first element in that contiguous memory
// is the address of that memory. In other words, the address of that union.
var addr *byte = &union[0]

// The second magic. Instead of pointing to bytes of memory, we can point
// to some useful type, T, by changing the type of the pointer to *T using
// unsafe.Pointer. In this case we want to interpret the union as member
// `guint32 *ui32v`. That is, T = (*C.guint32) and *T = (**C.guint32).
var cast **C.guint32 = (**C.guint32)(unsafe.Pointer(addr))

// The final step. We wanted the contents of the union, not the address
// of the union. Dereference it!
var guint32_star *C.guint32 = *cast

Credit goes to Alan Shen's article which described the cgo representation of a union in a way that finally made sense to me.

Problem

I'm working with this C struct on a 64 bit platform, trying to access the ui32v field in the value union: ``` struct _GNetSnmpVarBind { guint32 *oid; /* name of the variable */ gsize oid_len; /* length of the name */ GNetSnmpVarBindType type; /* variable type / exception */ union { gint32 i32; /* 32 bit signed */ guint32 ui32; /* 32 bit unsigned */ gint64 i64; /* 64 bit signed */ guint64 ui64; /* 64 bit unsigned */ guint8 *ui8v; /* 8 bit unsigned vector */ guint32 *ui32v; /* 32 bit unsigned vector */ } value; /* value of the variable */ gsize value_len; /* length of a vector in bytes */ }; ``` I could write a C wrapper function for each union element but for didactic purposes I'd rather work in Go. Here's how I'm trying to access the ui32v field: ``` func union_to_guint32_ptr(cbytes [8]byte) (result *_Ctype_guint32) { buf := bytes.NewBuffer(cbytes[:]) var ptr uint64 if err := binary.Read(buf, binary.LittleEndian, &ptr); err == nil { return (*_Ctype_guint32)(unsafe.Pointer(ptr)) } return nil } ``` However this gives an error cannot convert ptr (type uint64) to type unsafe.Pointer So how do I convert a uint64 to a Go type that points to a C guint32? I've tried various combinations of casting to a uintptr then casting to a *_Ctype_guint32, casting to a uintptr then using unsafe.Pointer, ... My reasoning is: I'm passed an array of 8 bytes. Convert that to a uint64, that's the memory address. Cast that to a pointer to a guint32 (ie a C array of guint32's), and return that as a result - that is the union field "value" as a guint32 *. Context Later I'll want to convert the C array of guint32's to a string utilising the value_len field, using a function I know already works: ``` guint32_star := union_to_guint32_ptr(data.value) result += OidArrayToString(guint32_star, data.value_len) ``` The C code is from gsnmp.

Original source