Enum and Flag with Go's iota
Post:2021-02-23 13:28:20
Tags:/
Go
/
Visit:
Enum
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| package main
import ( "fmt" )
type Color uint8
const ( Red = iota Green Blue )
func main() { fmt.Printf("%04b\n", Red) fmt.Printf("%04b\n", Green) fmt.Printf("%04b\n", Blue) }
|
Flag
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
| package main
import ( "fmt" )
type Color uint8
const ( Red = 1 << iota Green Blue )
func main() { var color Color
color |= Red fmt.Printf("%04b\n", color)
color |= Green fmt.Printf("%04b\n", color)
color &^= Red fmt.Printf("%04b\n", color)
fmt.Printf("%04b\n", Red) fmt.Printf("%04b\n", Green) fmt.Printf("%04b\n", Blue) }
|