我是否应该相信“ aligncheck”报告的尺寸

I have a struct defined as below:

type MyStruct struct {
    A [10]byte
    B uint64
    C uint16
}

And use with binary.Read as below

err = binary.Read(r, binary.BigEndian, &mystruct)  // mystruct is Mystruct type

I got the correct value for all fields in mystruct. And some following code read from Reader r all get correct result.

But the aligncheck side MyStruct could have size 24 but currently is 32. But for the binary byte read, it should take 20 bytes.

So I am not sure I just got luck to get the correct result or some part of the go tool chain would pack the struct to proper size?

As Tim Cooper stated in his comment, you're comparing two different representations of the same data. aligncheck tells you how much space is occupied in memory (heap/stack) by an instance of a struct, which is affected by struct field alignment and padding, for reasons explained at length elsewhere.

binary.Read and .Write, on the other hand, are not trying to store objects in memory with boundary alignment; they're just writing a stream of contiguous bytes. Therefor these will output the same size struct regardless of the order of fields, and it will always be the minimum size of the struct (because there is no padding added).

After revisited the binary's source code, it gets the real length of each field of the struct and then read the correct amount of data from r reader and assign the value to each field respectively. So it doesn't matter how the struct is aligned in the memory.