Constants and iota
Defining constants and using iota for enumerations
Constants and iota is a free Go Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Go Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What Are Constants?
A constant stores a value that never changes at runtime.
- Declared with
const - Must be computable at compile time
- Can be typed or untyped
- String, boolean, numeric, and rune literals are allowed
Typed vs Untyped Constants
Untyped constants have higher precision and are more flexible:
const Pi = 3.14159— untyped float, adapts to contextconst Pi float64 = 3.14159— typed float64- Untyped integer constants can be used with any integer type
Constant Expressions
Constants can be computed from other constants at compile time:
const (
KB = 1024
MB = 1024 * KB
GB = 1024 * MB
)iota: Auto-Incrementing Values
iota is a built-in counter that resets to 0 in each const block and increments by 1 for each constant in the block.
const (
Sunday = iota // 0
Monday // 1
Tuesday // 2
Wednesday // 3
)iota with Expressions
Combine iota with expressions for powerful enumerations:
const (
_ = iota // skip 0
KB = 1 << (10 * iota) // 1024
MB // 1048576
GB // 1073741824
)String Constants
String constants are immutable string values known at compile time:
const (
AppName = "GoApp"
AppVersion = "1.0.0"
Greeting = "Hello, " + AppName
)Boolean Constants
true and false are predeclared untyped boolean constants in Go. You can define your own boolean-typed constants for feature flags:
const (
EnableMetrics = true
DebugMode = false
)Constants vs Variables
Key differences:
- Constants are immutable — assignment after declaration is a compile error
- Constants cannot use non-constant expressions (e.g., function calls)
- Constants are more efficient — they may be inlined by the compiler
- Use constants for magic numbers to improve readability
Enumerated Types with iota
Create type-safe enumerations by combining a defined type with iota:
type Direction int
const (
North Direction = iota
East
South
West
)Stringer for Enum Readability
Implement the Stringer interface on your enum type so it prints human-readable names rather than raw integers. The stringer tool can auto-generate this code for you.
iota Bitmask Pattern
iota with bit shifts creates flag sets:
type Permission uint
const (
Read Permission = 1 << iota // 1
Write // 2
Execute // 4
)
func main() {
access := Read | Write // 3
_ = access
}Quick Check
What value does iota start at in each new const block?
Recap: Constants and iota
You now understand Go constants:
constdeclares immutable compile-time values- Untyped constants are flexible and high-precision
iotaauto-increments within a const block, starting at 0- Combine iota with expressions for file sizes, bit flags, and enums
- Create type-safe enumerations with a defined type + iota
Next: explicit type conversions and type aliases.
Frequently asked questions
Is the “Constants and iota” lesson free?
Yes — the full text of “Constants and iota” is free to read here on the web, and the Go Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Go Academy course, upgrade to CoddyKit PRO.
What will I learn in “Constants and iota”?
Defining constants and using iota for enumerations You practise Go Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Go Academy?
No prior experience is required. Go Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Constants and iota” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Go Academy lesson?
Yes. Every Go Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Declaring Variables in Go
- Go's Built-in Types
- Constants and iota
- Type Conversions in Go