GO does not natively support an enum
keyword; however, an enum-like syntax can be implemented using the help of iota
.
What is iota
?
iota
is an identifier that auto increments constants.
In the following code, the constant number is manually incremented:
1
2
3
4
5
| const (
ZERO = 0
ONE = 1
TWO = 2
)
|
however, it can be simplified to:
1
2
3
4
5
| const (
ZERO = iota // 0
ONE // 1
TWO // 2
)
|
If desired, the iota
keyword can be used on each line:
1
2
3
4
5
| const (
ZERO = iota // 0
ONE = iota // 1
TWO = iota // 2
)
|
If a const
keyword is used again, the iota
will reset to 0.
1
2
3
4
5
6
7
8
9
10
11
| const (
ZERO = iota // 0
ONE // 1
TWO // 2
)
const (
ZEROAGAIN = iota // 0
ONEAGAIN // 1
TWOAGAIN // 2
)
|
Implementing an enum in GO
Using a combination of iota
, int
and const
, the concept of an enum
can be applied.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
| package main
import "fmt"
type PizzaSize int
const (
SMALL PizzaSize = iota + 1 // 1
MEDIUM // 2
LARGE // 3
)
func (s PizzaSize) String() string {
return [...]string{"Small", "Medium", "Large"}[s-1]
}
func (s PizzaSize) Index() int {
return int(s)
}
func main() {
size := SMALL
fmt.Println(size) // Print : Small
fmt.Println(size.String()) // Print : Small
fmt.Println(size.Index()) // Print : 1
}
|