Constants and iota
Understand untyped constants and `iota`; constant rules are compile-time semantics, not ordinary runtime variables.
Canonical guidance
- constants are evaluated at compile time
- untyped constants have more flexibility until context gives them a type
iotais just the const-spec index inside one const block
Use when
- reviewing enum-like declarations
- checking representability or overflow at compile time
- explaining why a constant assignment compiles or fails
Avoid
- treating constants like mutable values
- assuming
iotacontinues across separate const blocks - relying on implicit narrowing without checking representability
Preferred pattern
type Level uint8
const (
Debug Level = iota
Info
Warn
Error
)
Anti-pattern
- using
iotain long opaque const blocks where the actual values are hard to audit
Explanation: This is tempting because it is terse, but opaque const blocks make versioning and review harder than explicit values or short grouped declarations.
Why
- many surprising compile errors around numbers come from constant semantics rather than variable semantics