-
Notifications
You must be signed in to change notification settings - Fork 1
/
enum.go
39 lines (31 loc) · 912 Bytes
/
enum.go
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
27
28
29
30
31
32
33
34
35
36
37
38
39
package main
import "fmt"
type ServerState int
const (
UNDEPLOYED ServerState = iota
IN_PROGRESSING
DEPLOYED
)
func validate(state ServerState) error {
if state == UNDEPLOYED || state == IN_PROGRESSING || state == DEPLOYED {
return nil
}
return fmt.Errorf("%+v is not a valid state", state)
}
func main() {
const NEW_STATE ServerState = 99
const HACKING_STATE int = 2
// IN_PROGRESSING works well, because it is in the enum
if err := validate(IN_PROGRESSING); err != nil {
fmt.Printf("%+v", err)
}
// NEW_STATE works well, because it is a var of type ServerState
if err := validate(NEW_STATE); err != nil {
fmt.Printf("%+v", err)
}
// the following code wont compile, because it is not a var of type ServerState
// this is due to the use of 'iota', compiler will ensure only specified enums are used
if err := validate(HACKING_STATE); err != nil {
fmt.Printf("%+v", err)
}
}