Stage 1 · Code
Language Depth II
Constants and iota
Learn how Go represents values that never change, why some constants stay flexible until you use them, and how `iota` helps you build clean sequences of related values.
Why Constants Exist
Some values in a program are not supposed to drift. A port number might be configurable, but the number of days in a week is not. A retry counter changes, but the label you use to represent 'warning' does not. Constants are Go's way of saying, 'this value is part of the rules, not part of the changing state.'
That matters because fixed values communicate intent. If a reader sees const maxWorkers = 8, they learn that eight is a chosen rule. If they see maxWorkers := 8, they must wonder whether the value might later be reassigned.
A constant is a good fit when changing the value during program execution would make no sense. If the value is meant to vary from request to request, user to user, or run to run, you probably want a variable instead.
Typed and Untyped Constants
Go gives constants an interesting superpower: some constants stay untyped until they are used. That sounds abstract, but it solves a practical problem. A small fixed value like 60 can fit naturally into different numeric contexts without you spelling out a conversion every time.
| Kind | Example | What it means |
|---|---|---|
| Untyped constant | const secondsPerMinute = 60 | The value stays flexible until the surrounding code gives it a concrete type |
| Typed constant | const timeoutSeconds int = 5 | The constant already has a specific type from the moment it is declared |
This is one reason constants feel smoother than ordinary variables in Go. Variables always have a concrete type right away. Untyped constants can wait and adapt, as long as the final use still makes sense.
iota as a Counting Machine
Now for the feature that makes constant blocks especially pleasant: iota. The easiest mental model is not 'magic enum keyword.' Think of iota as a small counter that starts at 0 inside a const block and increases by 1 on each new line in that block.
Go does not have a dedicated enum keyword, so iota is the language's simple building block for enum-like groups of related constants. It is not limited to enums, but that is one of its most common jobs.
Tracing a Real iota Block
Let's slow that down completely. Many beginners see SeverityInfo and think, 'where did its value come from? I never wrote one.' The answer is: Go reused the previous expression and only advanced iota.
One more important rule: iota resets for each separate const block. It does not keep counting forever across the whole file.
Useful iota Tricks
Once you are comfortable with simple counting, iota can do a little more. Because it is just a number inside each constant expression, you can skip values, multiply them, or shift bits with it.
- Use
_ = iotawhen you want to skip the zero position. - Use arithmetic like
10 * iotaor1 << iotawhen the sequence follows a pattern larger than simple0, 1, 2, 3. - Keep the block readable.
iotais helpful when it reduces repetition, not when it turns a simple declaration into a puzzle.
A named type such as type Severity int makes the intent clearer than using plain integers everywhere. The constants then become a small controlled vocabulary for that domain.
Quiz
What is the most useful mental model for `iota`?
Practice
Define a named type `LogLevel` based on `int`, then declare constants `LogDebug`, `LogInfo`, `LogWarn`, and `LogError` using a single `const` block with `iota`. `LogDebug` should be `0`, and each later constant should increase by one.
Summary
- Constants represent values that should not change during program execution.
- Untyped constants stay flexible until context gives them a concrete type, while typed constants commit to a type immediately.
iotais a per-const-block counter that starts at0and increases line by line.- Go uses
iotaas a clean enum-like pattern because the language has no nativeenumkeyword. - You can reset, skip, multiply, and shift
iotavalues, but the clearest use is usually the best one.
Mark this lesson complete to store local progress and unlock a cleaner resume path the next time you visit.