Enumerated Type
Examples in
Go
// The iota keyword represents successive integer constants 0, 1, 2,…
// It resets to 0 whenever the word const appears in the source code,
// and increments after each const specification.
const (
C0 = iota
C1
C2
)
fmt.Println(C0, C1, C2) // "0 1 2"
// To start a list of constants at 1 instead of 0, you can use iota in an arithmetic expression.
const (
C1 = iota + 1
C2
C3
)
fmt.Println(C1, C2, C3) // "1 2 3"
const (
C1 = iota + 1
_
C3
C4
)
fmt.Println(C1, C3, C4) // "1 3 4"
// Complete enum type with strings [best practice]
type Direction int
const (
North Direction = iota
East
South
West
)
func (d Direction) String() string {
return [...]string{"North", "East", "South", "West"}[d]
}