Beginner Fundamentals
Constants
A constant holds a value that never changes while the program runs. Constants are declared with the const keyword.
Declaring constants
const pi = 3.14159
const greeting = "Hello"
const maxUsers int = 100
Trying to reassign a constant causes a compile error, which protects values that should stay fixed.
Grouping constants
You can group several constants in one block.
const (
appName = "LearnGo"
version = "1.0"
debug = false
)
iota
iota is a counter that helps build sequences. It starts at 0 in each const block and increases by one per line.
const (
Sunday = iota // 0
Monday // 1
Tuesday // 2
Wednesday // 3
)
You can use expressions with iota too:
const (
_ = iota // skip 0
KB = 1 << (10 * iota) // 1024
MB // 1048576
)
fmt.Println(KB, MB)