Constants and iota in Go
Fast recall
Quick revision
Core answer
Whether you know constants are compile-time values and can model a small enum safely.
Expecting iota to persist across blocks or using an untyped constant where a runtime value is required.
Mention that iota resets to zero in each const block and constants can remain untyped until context gives them a type.
Interview-ready guide
Interview answer
In Go, a constant is a value the compiler can determine and that the program cannot change at runtime. Constants can be strings, booleans, or numeric values. They are useful for names such as
MaxRetries,Pi, or a fixed status value because the name explains the meaning and accidental reassignment is impossible.A constant may be typed, such as
const Timeout time.Duration = 5 * time.Second, or untyped, such asconst attempts = 3. An untyped numeric constant has more flexibility because Go gives it a concrete type only when the surrounding expression requires one, provided the value can be represented by that type.iotais a counter available inside aconstdeclaration block. It starts at zero for the first constant specification and increases by one for each following specification. It resets when a newconstblock begins; it is not a global counter and it does not keep increasing across the package.For example,
type Level int; const (Low Level = iota; Medium; High)producesLow = 0,Medium = 1, andHigh = 2. The omitted expressions repeat the previous expression, soMediumandHighreuseLevel = iotawith the new value ofiota.This pattern is convenient for small internal enum-like sets and for bit flags such as
Read = 1 << iota,Write, andExecute. Giving the constants their own type also prevents unrelated integer values from being passed accidentally, although Go still does not provide a fully closed enum type.The main risk is treating generated numbers as permanent business data. Inserting a new line in the middle of an
iotablock changes the values below it, which can break stored database values, API contracts, or file formats. For externally visible values, I normally assign explicit numbers instead.My practical rule is to use ordinary constants for fixed named values, use
iotawhen the sequence itself is the useful pattern, and use explicit values whenever numeric stability matters outside the program.
Constants and iota in Go
Direct Answer
A Go constant is an immutable compile-time value. In a const block, iota starts at zero and increments for each constant specification, making it useful for enum-like values and bit flags.
Deep Dive & Architecture
Use a named constant for a value that is fixed at compile time.
Use iota when a private group of related values follows a clear sequence or formula, such as internal states or bit flags.
Prefer explicit numbers when values are stored or shared outside the program, and use variables for values loaded or calculated at runtime.
The most dangerous mistake is assuming iota values will stay stable after the declaration is edited. Inserting a new specification changes every later value. A blank-identifier line also consumes a position, and a new const block resets the sequence. Use explicit numbers for stored or public values and test important mappings.
Code Example
type Level int
const (
Low Level = iota
Medium
High
)
Common Mistakes & Pitfalls
Expecting iota to persist across blocks or using an untyped constant where a runtime value is required.
Check your understanding
Answer these without looking back at the article.
- How do iota-generated constants compare with explicit constants in Go?
- When should you use constants and iota in a Go project?
- What common mistakes should you avoid with iota in Go?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.