Constants & iota
A constant is a value fixed at compile time that can never change, and iota is Go's built-in counter for generating sequences of related constants like enums and bit flags.
Learn Constants & iota in our free Go course — an interactive lesson with runnable examples, a practice exercise and a quick reference.
Part of the free Go course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
What You'll Learn in This Lesson
1️⃣ Declaring Constants
You declare a constant with the const keyword. Because the value is fixed at compile time , the compiler can compute expressions like 60 * 60 * 24 up front — but it will reject anything it can't evaluate, like a function-call result. You can declare constants one at a time or group related ones in a const ( ... ) block. Reassigning a constant is a compile error, which makes constants a great way to lock down values that must never drift.
2️⃣ Typed vs Untyped Constants
An untyped constant (like const big = 1 << 40 ) has no fixed type until you actually use it — it adapts to whatever context it lands in, so the same constant can become a float64 in one place and an int64 in another. A typed constant ( const maxRetries int = 5 ) is locked to one type, so mixing it with a different type needs an explicit conversion. Leaving constants untyped is usually the more flexible choice.
3️⃣ iota: The Enumerator
iota is a built-in counter that resets to 0 at the start of every const block and increases by 1 for each line — whether or not iota appears on that line. That makes it perfect for enum-style constants like the days of the week. You can skip a value by assigning a line to the blank identifier _ , and you can shift the whole sequence with arithmetic such as 1 << (10 * iota) to build a kilobyte / megabyte ladder.
4️⃣ Bit Flags with iota
Shifting 1 << iota gives you 1, 2, 4, 8, ... — each a single distinct bit. Because no two flags share a bit, you can pack several on/off options into one integer: combine them with | (OR), test them with & (AND), and add one with |= . This is the classic permission-bits pattern ( Read , Write , Execute ).
🎯 Your Turn
Fill in the blank so Small , Medium , and Large become 0 , 1 , and 2 . Remember that only the first line needs iota — the rest inherit it.
Extend the byte-size ladder. In one const block, skip 0 , then use 1 << (10 * iota) for KB , MB , GB , and add a TB line. Print TB and match the expected output.
Practice quiz
When is a const value fixed?
- At run time
- On first use
- At compile time
- When garbage collected
Answer: At compile time. A constant is fixed at compile time, so the compiler can bake its value directly into the program.
Which of these is a valid constant declaration?
- const Pi = 3.14159
- const start = time.Now()
- const r = rand.Int()
- const now = os.Getenv("X")
Answer: const Pi = 3.14159. Constants must be compile-time values. A function call result like time.Now() is not allowed.
What value does iota have on the first line of a const block?
- 1
- -1
- Whatever you assign
- 0
Answer: 0. iota starts at 0 on the first line of a const block and increments by 1 for each subsequent line.
Given const ( Sunday = iota; Monday; Tuesday; Wednesday ), what is Wednesday?
- 0
- 3
- 1
- 4
Answer: 3. iota is 0 for Sunday, then 1, 2, 3 — so Wednesday is 3.
What does an untyped constant like const big = 1 << 40 give you?
- No fixed type until used; it adapts to context
- A fixed int type
- A float64 always
- A compile error
Answer: No fixed type until used; it adapts to context. An untyped constant has no fixed type until used, so the same constant can become a float64 or an int64.
With 1 << iota for Read, Write, Execute, what are their values?
- 0, 1, 2
- 1, 2, 3
- 1, 2, 4
- 2, 4, 8
Answer: 1, 2, 4. 1 << iota gives 1, 2, 4 — each a distinct single bit, ideal for combinable flags.
Given Read=1, Write=2, what does Read | Write equal?
- 1
- 3
- 2
- 0
Answer: 3. Bitwise OR of 001 and 010 is 011, which is 3.
What does an iota line that omits its expression do?
- Sets the value to 0
- Causes a compile error
- Copies the previous value exactly
- Repeats the previous line's expression with the new iota
Answer: Repeats the previous line's expression with the new iota. A line without an expression repeats the previous line's expression, evaluated with the new iota value.
How do you skip the value 0 at the start of an iota block?
- start = 1
- Assign the first line to _ (e.g. _ = iota)
- Use iota--
- You cannot skip it
Answer: Assign the first line to _ (e.g. _ = iota). Assigning the first line to the blank identifier _ discards 0 so the real values start at the next iota.
Using 1 << (10 * iota) with _ skipping 0, what is the value after KB, MB, GB (the TB line)?
- 1073741824
- 1048576
- 1099511627776
- 549755813888
Answer: 1099511627776. iota reaches 4 on the TB line, so 1 << 40 = 1099511627776.