Variable vs Constant size

January 15, 2025

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 if T 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 to Alignof, Offsetof, and Sizeof are compile-time constant expressions of type uintptr if their arguments (or the struct s in the selector expression s.f for Offsetof) 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)


Share this

Direct to your inbox, daily. I respect your privacy .

Unsure? Browse the archive .

Get daily content like this in your inbox!

Subscribe