gst

github.com/tyranron/go-sizeof-tip

go get github.com/tyranron/go-sizeof-tip

Readme

Sizeof tip for Go types

Just a small tip to check yourself when organizing structs.

import . "unsafe"

Fixed sizes

TypeSizeof() bytes
struct{}0
[0]Type0
bool1
int8, uint8, byte1
int16, uint162
int32, uint32, rune4
float324
int, uint4 or 8
int64, uint648
float648
complex648
uintptr8
*struct{}, *Type8
map[Type1]Type28
chan Type8
func()8
string16
complex12816
[]Type24

Array

Formula: Sizeof([N]Type) = N * Sizeof(Type)

Examples:

TypeSizeof() bytes
[5]bool5
[2][]bool{}48

Struct

Struct size depends on how struct is packed.

Formula:
Sizeof(struct{T1; ... TN}) = StructPackSize * PacksNum,
where StructPackSize = min(max(Sizeof(T1), ... Sizeof(TN)), Sizeof(uintptr))
and PacksNum depends on how struct fields are packed due to their order.

Examples:

TypeStructPackSizePacksNumSizeof() bytes
struct{a struct{}}010
struct{a struct{}; b bool}111
struct{b bool; u int32}428
struct{a bool; b bool; u int32}428
struct{u int32; a bool; b bool}428
struct{a bool; u int32; b bool}4312
struct{a bool; s string; b bool}8432

Jump To