Back to Concepts Roadmap
🟡 Level 1 — Fundamentals
Foundation
Constants
Understand compile-time constants, untyped arbitrary precision, and the `iota` auto-incrementing enumerator.
Real-World Analogy (Mental Model)
“Constants are like blueprints carved into stone before the building is even constructed. They can never be altered once created.”
Architecture & Memory MapVisual Model
const (
Sunday = iota // 0
Monday = iota // 1
Tuesday = iota // 2
Wednesday = iota // 3
)Key Concepts & Rules To Remember
- Declared with `const` keyword and evaluated strictly at compile time.
- Untyped constants have infinite mathematical precision until assigned to a typed variable.
- `iota` is an auto-incrementing integer index that simplifies enum definitions.
Step-by-Step Code
1. Declaring Constants & iota
`iota` resets to 0 whenever the `const` keyword appears and increments by 1 on each subsequent line in the block.
example.goGo 1.24+
package main
import "fmt"
const (
StatusPending = iota // 0
StatusActive // 1 (iota continues)
StatusClosed // 2
)
const (
_ = 1 << (10 * iota)
KB // 1024
MB // 1048576
GB // 1073741824
)
func main() {
fmt.Println(StatusActive) // 1
fmt.Printf("1 GB = %d bytes\n", GB)
}Common Beginner Pitfalls & Mistakes
Mistake: Trying to assign a runtime function return value to a constant (e.g. `const now = time.Now()`).
✅ Correct Way: Constants can ONLY be assigned values that are known at compile time.
Self Assessment
Knowledge Check
Verify your understanding with these interactive practice questions.
Quizzes
Quick checks for understanding
Multiple-choice with inline explanations—expand to see why.
When are Go constants evaluated?
Finished this lesson?
Mark it as complete to track your overall Go mastery.