Iota
Go provides a special keyword called iota that makes it easy to define a sequence of related integer constants.
How it works
iota starts at 0 and increments by 1 for every constant defined in a const block. It resets to 0 whenever a new const block begins.
type emailStatus int
const (
EmailBounced emailStatus = iota // 0
EmailInvalid // 1
EmailDelivered // 2
EmailOpened // 3
)
Benefits
- Less Repetition: You don’t have to manually assign
0, 1, 2...to every value. - Readability: It clearly communicates that these values are part of a related sequence of states.
- Type Safety: Like string-based enums, using a custom type like
emailStatusprevents you from accidentally passing a plainintto a function expecting a specific status.