Use answers in Sizeof struct in Go, but start with a nil
pointer to the type:
type Coord3d struct {
X, Y, Z int64
}
// Option 1:
ptrType := reflect.TypeFor[*Coord3d]()
valueType := ptrType.Elem()
size := valueType.Size()
fmt.Println(size) // prints 24
// Option 2:
size = unsafe.Sizeof(*(*Coord3d)(nil))
fmt.Println(size) // prints 24
https://play.golang.com/p/Bj8hvQ3aPeP
The simplest approach is to rely on compiler optimizations.
size = unsafe.Sizeof(Coord3d{})
fmt.Println(size) // prints 24
Because the value of Coord3d{}
is not used, the compiler does not allocate it.