13.1 unsafe.Sizeof
, Alignof
, and Offsetof
The unsafe.Sizeof
function reports the size in bytes
of the representation of its operand,
which may be an expression of any type; the expression is not
evaluated.
A call to Sizeof
is a constant expression of type
uintptr
,
so the result may be used as the dimension of an array
type, or to compute other constants.
import "unsafe" fmt.Println(unsafe.Sizeof(float64(0))) // "8"
Sizeof
reports only the size of the fixed part of each data structure, like the pointer and length of a string, but not indirect parts like the contents of the string. Typical sizes for all non-aggregate Go types are shown below, though the exact sizes may vary by toolchain. For portability, we’ve ...
Get The Go Programming Language now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.