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.
From the CGO documentation:
To access a struct, union, or enum type directly, prefix it with struct_, union_, or enum_, as in C.struct_stat.
So I guess (not tested) the code might be something similar to:
myUint32var := somePtrTo_GNetSnmpVarBind.union_guint32
for accessing the guint32 member of the union of the struct pointed to by somePtrTo_GNetSnmpVarBind
The solution was first to cast to uintptr, then cast to unsafe.Pointer ie two separate casts:
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 {
uptr := uintptr(ptr)
return (*_Ctype_guint32)(unsafe.Pointer(uptr))
}
return nil
}
I checked this by comparing results with a command line tool, and it's returning correct results.
Context
// gsnmp._Ctype_gpointer -> *gsnmp._Ctype_GNetSnmpVarBind
data := (*C.GNetSnmpVarBind)(out.data)
switch VarBindType(data._type) {
case GNET_SNMP_VARBIND_TYPE_OBJECTID:
result += "GNET_SNMP_VARBIND_TYPE_OBJECTID" + ":"
guint32_star := union_to_guint32_ptr(data.value)
result += OidArrayToString(guint32_star, data.value_len)
Sonia already answered her own question, I just want to provide the reason for why two type conversions are necessary.
From the documentation for unsafe.Pointer:
1) A pointer value of any type can be converted to a Pointer.
2) A Pointer can be converted to a pointer value of any type.
3) A uintptr can be converted to a Pointer.
4) A Pointer can be converted to a uintptr.
Since var ptr uint64
is not a pointer (as type uint64
is not a pointer), ptr
cannot be converted directly to unsafe.Pointer
using rule 1. Therefore it is necessary to first convert ptr
to uintptr
, then from uintptr
to a Pointer
following rule 3.