iota
A Go keyword usable only inside a const block, where it counts 0, 1, 2 and so on, used to number a set of related constants.
In more detail
Go has no separate enum keyword; a named type plus a const block with iota does that job. Putting the unknown state first makes the zero value mean unknown, which is the safe default.
Example
const (
unknown state = iota
healthy
degraded
down
)Related terms
- constant — A name for a value that is set once and is not meant to change while the program runs.
- named type — A new type you declare with the type keyword, based on an existing one, such as type state int.
- zero value — The value Go gives a variable automatically when you declare it without one: 0 for numbers, "" for strings, false for booleans and nil for pointers, slices and maps.
Taught in
- Variables, Typing, and Zero Values — Concurrent Systems Programming with Go