Enums
Go does not have a built-in enum
type like some other languages (e.g., C or Java), but you can achieve similar functionality using a combination of constants and the iota
keyword.
Iota
Go’s iota
identifier is used in const
declarations to simplify definitions of incrementing numbers.
const ( East = iota West North South)
fmt.Println(South) // 3
Using an enum
To create an enum
, you first have to declare a variable that will hold the enum.
type Shape int
When initialising the constant, you can assign the Shape
variable to it
const ( East Shape = iota West North South)
Now you can pass the Shape
*enum as an argument to functions
func printShape(s Shape) { fmt.Print(s)}