when to use constants and iota in Go
Fast recall
Quick revision
Core answer
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.Whether you can explain How do constants and iota work in Go in plain language and apply it to a small example.
Giving a slogan about How do constants and iota work in Go without checking its scope, boundary cases, or trade-offs.
State the rule, walk through one concrete example, and name the test or edge case you would verify.
Interview-ready guide
Interview answer
I use a Go constant when a value has a stable meaning and should never be reassigned while the program is running. Examples include retry limits, mathematical values, configuration defaults compiled into the binary, and named states. Replacing unexplained literals with names such as
MaxRetriesalso makes the code easier to review.I use
iotafor a related sequence of constants where the generated pattern is more important than the exact number. A small internal state type is a common example:type State int; const (Pending State = iota; Running; Done). The values become zero, one, and two without repeating each expression.The custom
Statetype matters as much as the counter. It documents what the values represent and stops a variable of an unrelated named type from being passed by mistake. I can then attach methods such asStringor validation behaviour to that type while keeping the stored value compact.iotais also useful for bit flags. Withconst (Read Permission = 1 << iota; Write; Execute), each constant receives a different bit. Values can be combined with the bitwise OR operator and checked with the bitwise AND operator, which is clearer than manually maintaining powers of two.I avoid
iotawhen the numbers are part of a public API, database record, message schema, or file format. Adding a constant in the middle changes every following generated value. In those cases I write explicit values, even though it is slightly more verbose, because compatibility is more important than reducing repetition.I also do not use constants for information that must be calculated or loaded at runtime. A value returned by a function, read from an environment variable, or created with
time.Now()must be stored in a variable because it is not a compile-time constant expression.My default is therefore simple: use a named constant for a fixed value, use
iotafor a private and naturally sequential set, and use explicit stable values at system boundaries.
when to use constants and iota in Go
Direct Answer
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.
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 Status int
const (
Pending Status = iota // 0
Running // 1
Done // 2
)
Common Mistakes & Pitfalls
Giving a slogan about How do constants and iota work in Go without checking its scope, boundary cases, or trade-offs.
Check your understanding
Answer these without looking back at the article.
- How do constants and iota work in Go?
- How do iota-generated constants compare with explicit constants in Go?
- 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.