Can constants be computed at runtime in Go, and what are the limitations of const compared to var?
Last updated
No: constants must be computable at compile time from literals and constant expressions, so
const t = time.Now()won't compile.Constants are limited to booleans, numbers, strings, and runes. Slices, maps, structs, and pointers need
var.Untyped constants have high precision and adapt to context (e.g. usable as
intorfloat64without conversion). Usevarfor runtime-computed or changing values.