common iota mistakes in Go
Fast recall
Quick revision
Core answer
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.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
A common mistake is assuming the number produced by
iotawill remain stable forever. The value depends on a constant's position in itsconstblock, so inserting another specification above it changes the generated number even though the constant's name and type remain unchanged.For example, suppose
Pending,Running, andDoneare stored as zero, one, and two in a database. IfPausedis later inserted betweenPendingandRunning,Runningbecomes two andDonebecomes three. Old rows are then interpreted as the wrong states, and the compiler cannot detect that compatibility problem.I prevent this by assigning explicit values whenever constants cross a process boundary or are persisted:
Pending State = 0,Running State = 1, andDone State = 2.iotais safer for private implementation details where renumbering has no external effect.Another subtle point is that
iotaadvances for every constant specification, including a line that uses the blank identifier. Inconst (_ = iota; KB = 1 << (10 * iota)), the first line deliberately consumes zero, soKBuses one. Removing that placeholder changes the calculation.The zero value also deserves attention. If zero means a valid state such as
Pending, an uninitialized variable silently has that state. Sometimes that is useful, but sometimes I reserve zero asUnknownso missing initialization is visible instead of being treated as a legitimate business value.I would test the actual numeric values when they form a contract, not only their printed names. A focused test that asserts
Running == 1andDone == 2catches accidental renumbering during review.The key rule is that
iotaremoves repetitive numbering, but it does not promise stable numbering. I choose it only when that trade-off is acceptable.
common iota mistakes in Go
Direct Answer
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.
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.
Write down the expected and actual numeric values first. Reduce the code to one const block and expand every omitted expression. Remember that iota starts at zero, advances for each specification, and resets in a new block. If a value is stored or exposed through an API, keep it stable with explicit numbers and regression tests.
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
const (
Pending State = iota // 0
Paused // 1 (new)
Running // 2 (was 1)
)
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?
- When should you use constants and iota in a Go project?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.