If you don't know what the padding rules are (and padding is driven by a need for alignment), then a safe approach is to declare struct members in decreasing (biggest first) order of size -- this works fine for structs without any exported members. However, it's better to just properly learn what the rules are, and apply those sensibly, since for exported struct members are often poorly presented if they're simply arranged in size order. A good example of what to do:
type Node struct {
X, Y, Z int8
B bool
Flag uint32
Next *Node
}
This struct will be precisely 12 bytes on typical 32-bit architectures, and precisely 16 bytes on typical 64-bit architectures, but requires absolutely no padding, because the fields are well packed within the alignment rules.
iirc, the required alignment for any type T is: min(sizeof(T), sizeof(uintptr)). I'm not sure how 8-byte numerics are handled on 32-bit platforms, though -- if those must be aligned, then the rule is just min(sizeof(T), 8).
So on 32-bit architectures (and the upcoming amd64 nacl target), no type will require being aligned to greater than a multiple of 4 bytes, and 1-byte types never need alignment. Since in the above Node type, there are four 1-byte fields, Flag, which needs to be aligned to a multiple of 4, will need no padding for alignment, and since the first five fields add up to 8 bytes total, that means that the pointer that follows will have 8 bytes of alignment already -- precisely what is needed for 64-bit pointers, and more than enough for 4-byte pointers. Further, since the whole struct is a multiple of 4 bytes of 32-bit architectures, and a multiple of 8 bytes on 64-bit platforms, a slice/array of Node will not require any padding between elements.
That said, don't worry about any padding that gets added at the end of a struct -- it's fairly inconsequential, and it's certainly not worth getting rid of fields (or adding junk fields -- which would be the same as padding) just to avoid that padding; large intra-field padding is where things get inefficient. For example, `[]struct { X byte; Y int64; Z byte }` requires 24 bytes per element on 64-bit platforms (7 bytes of padding following X to align Y to a multiple of 8, and another 7 bytes of trailing padding after Z to align elements to a multiple of 8). Compare this to XZY or YXZ orderings, which would require only 16 bytes per element.