iota-generated constants versus explicit constants
Fast recall
Quick revision
Core answer
iota is concise for private sequences and formulas, while explicit assignments make every numeric value visible and stable. Use iota when the pattern matters and explicit values when databases, APIs, files, or other systems depend on the numbers.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
The useful comparison is between constants generated with
iotaand constants that have explicit values. Both approaches create immutable compile-time values, so there is no meaningful runtime speed advantage. The decision is about readability, maintenance, and whether the numeric values must remain stable.With
iota, an enum-like declaration is concise:const (Pending State = iota; Running; Done). Go evaluatesiotaas zero, one, and two, and omitted expressions repeat the previous expression. This works well when the constants are related, private to the program, and naturally sequential.The explicit alternative is
const (Pending State = 0; Running State = 1; Done State = 2). It is more repetitive, but each value is visible and remains unchanged if a new constant is inserted between existing lines. That stability is important when the numbers are stored or exchanged with another system.iotabecomes especially valuable when the relationship is a formula rather than simple counting. For bit flags,Read Permission = 1 << iotafollowed byWriteandExecuteclearly expresses the power-of-two pattern and avoids manually calculating one, two, and four.Explicit values are better for HTTP or RPC contracts, database enums, metrics labels that encode numbers, and file formats. In those situations, changing a value can break another deployment or reinterpret existing data, so a few repeated assignments are a reasonable cost.
Neither approach gives Go a closed enum. Code can still convert another integer to the named type, so validation may be required at input boundaries. I would not claim that
iotaautomatically makes every value safe.My default is
iotafor internal sequences and bit patterns, but explicit constants for durable or externally visible identifiers. The boundary of the program, rather than syntax preference, determines the choice.
iota-generated constants versus explicit constants
Direct Answer
Both approaches create compile-time constants; there is no meaningful runtime performance difference. iota is concise for private sequences and formulas, while explicit assignments make every numeric value visible and stable. Use iota when the pattern matters and explicit values when databases, APIs, files, or other systems depend on the numbers.
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.
Both approaches create compile-time constants; there is no meaningful runtime performance difference. iota is concise for private sequences and formulas, while explicit assignments make every numeric value visible and stable. Use iota when the pattern matters and explicit values when databases, APIs, files, or other systems depend on the numbers.
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 State int
// Generated
Pending State = iota // 0
Running // 1
Done // 2
// Explicit
Pending State = 0
Running State = 1
Done State = 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?
- 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.