I\'m working with this C struct on a 64 bit platform, trying to access the ui32v field in the value union:
struct _GNetSnmp
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.