Roger already showed how to use Sizeof method from the unsafe package. Make sure you read this before relying on the value returned by the function:
The size does not include any memory possibly referenced by x. For
instance, if x is a slice, Sizeof returns the size of the slice
descriptor, not the size of the memory referenced by the slice.
In addition to this I wanted to explain how you can easily calculate the size of any struct using a couple of simple rules. And then how to verify your intuition using a helpful service.
The size depends on the types it consists of and the order of the fields in the struct (because different padding will be used). This means that two structs with the same fields can have different size.
For example this struct will have a size of 32
struct {
a bool
b string
c bool
}
and a slight modification will have a size of 24 (a 25% difference just due to a more compact ordering of fields)
struct {
a bool
c bool
b string
}
As you see from the pictures, in the second example we removed one of the paddings and moved a field to take advantage of the previous padding. An alignment can be 1, 2, 4, or 8. A padding is the space that was used to fill in the variable to fill the alignment (basically wasted space).
Knowing this rule and remembering that:
- bool, int8/uint8 take 1 byte
- int16, uint16 - 2 bytes
- int32, uint32, float32 - 4 bytes
- int64, uint64, float64, pointer - 8 bytes
- string - 16 bytes (2 alignments of 8 bytes)
- any slice takes 24 bytes (3 alignments of 8 bytes). So
[]bool
, [][][]string
are the same (do not forget to reread the citation I added in the beginning)
- array of length
n
takes n
* type it takes of bytes.
Armed with the knowledge of padding, alignment and sizes in bytes, you can quickly figure out how to improve your struct (but still it makes sense to verify your intuition using the service).
unsafe.Sizeof
and it didn't work." – Irrupt