In dealing with Linux netlink messages I need to decode and encode uint16, 
uint32, and uint64 numbers that are in an arbitrary aligned byte buffer in 
an arbitrary position. In any case, these numbers are in native endianess, 
so I would like to avoid having to go through encoding/binary.

buff := bytes.NewBuffer(/* some data */)

// ...

func foo() uint32 {
    var s struct {
        _ [0]uint32
        b [4]byte
    }
    r.buff.Read(s.b[:])
    return *(*uint32)(unsafe.Pointer(&s.b[0]))
}

Will the go compiler (1.19+) allocate on the stack with the correct 
alignment for its element b, so that the unsafe.Pointer operation correctly 
works on different CPU architectures?

Or is this inefficient anyway in a subtle way that my attempt to avoid 
non-stack allocations is moot anyway?

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/ea8e7801-6caf-466b-8543-cfccfc76dd02n%40googlegroups.com.

Reply via email to