You’ll likely remember that certain built-in functions can sometimes evaluate to constants, rather than variables. For example:
var x [10]int
var y []int
len(x) // evaluates to a constant at compile time
len(y) // evaluates to a variable at runtime
Package unsafe
…
A (variable of) type
T
has variable size ifT
is a type parameter, or if it is an array or struct type containing elements or fields of variable size. Otherwise the size is constant. Calls toAlignof
,Offsetof
, andSizeof
are compile-time constant expressions of typeuintptr
if their arguments (or the structs
in the selector expressions.f
forOffsetof
) are types of constant size.
So here we have a few other special case examples of functions that can evaluate to constants at compile time, depending on the arguments passed to them.
Quotes from The Go Programming Language Specification Language version go1.23 (June 13, 2024)