Declare a constant with the const keyword. Its value is fixed when the program compiles and can never change. You can group related constants in a const ( ... ) block. Inside a group, iota starts at 0 and goes up by one on each line, which makes it the usual way to build enum-like values.
A constant without a stated type is an untyped constant, so it takes the type the context needs. That is why Pi works with both float32 and float64 values. Only values the compiler can work out, such as numbers, strings and booleans, can be constants, so a slice cannot be one.
const Pi = 3.14159
const MaxUsers = 100
// Grouped constants
const (
StatusOK = 200
StatusError = 500
StatusPending = 202
)
// iota for auto-incrementing constants
const (
Sunday = iota // 0
Monday // 1
Tuesday // 2
)
This answer doesn't lend itself to a diagram - it reads best . No credits were charged.
Why there's no diagram: “”
The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓
The diagram below the answer is the concept . Jump to it ↓