Let us discuss how to work with enumerators in Go.
Golang Enum
The syntax for declaring an enum in Go is as shown:
const (
variableenum_name = iota
variable2
variable3
…
)
Suppose we want to represent the days of a week. The most basic method is to create a mapping of the desired values.
An example:
Sunday int = 1
Monday = 2
Tuesday = 3
Wednesday = 4
Thursday = 5
Friday = 6
Saturday = 7
)
We can also represent the days as a string mapping as shown:
Sunday string = "sun"
Monday = "mon"
Tuesday = "tue"
Wednesday = "wed"
Thursday = "thur"
Friday = "fri"
Saturday = "sat"
)
Now, you may ask? What is wrong with representing the days of the week as shown above? First, it seems repetitive and very prone to conflicts.
For example, suppose we have two enums as:
const (
Sunday int = 1
Monday = 2
Tuesday = 3
Wednesday = 4
Thursday = 5
Friday = 6
Saturday = 7
)
// two
const (
driver int = 1
chef = 2
)
If we check for comparison, as:
The code returns true. This should not be the case.
We can fix this by creating an enum describing days of the week. An example code is as shown:
funcmain() {
type DaysOfWeek int64
const (
Sunday DaysOfWeek = 1
Monday = 2
Tuesday = 3
Wednesday = 4
Thursday = 5
Friday = 6
Saturday = 7
)
}
Here, we define a new type called DaysOfWeek() with an integer as the base type. This helps to define the const values as their type.
To ensure uniqueness in the values, we can use the iota keyword. The keyword will create a successive set of unique constant values:
import "fmt"
funcmain() {
type DaysOfWeek int64
const (
Sunday DaysOfWeek = iota
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
)
}
The previous code replaces the values with a set of successive integer values.
Once we declare an enum, we can create a variable of that type as shown:
import"fmt"
funcmain() {
typeDaysOfWeekint64
const (
Sunday DaysOfWeek = iota
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
)
var d DaysOfWeek
d = Thursday
if (d == Thursday) {
fmt.Println("Day of the week: ", d)
}
}
In the previous code, we declare a variable d with type DaysOfWeek(). We then assign the value to the type. Next, we can check if the value is equal to one of the enum.
The resulting output is as:
Enum String Method
Suppose we want to add a more descriptive message. We can add a method for the DaysOfWeek type as shown:
import"fmt"
typeDaysOfWeekint64
const (
Sunday DaysOfWeek = iota
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
)
func (d DaysOfWeek) String() string {
return []string{"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}[d]
}
funcmain() {
var day DaysOfWeek = Thursday
switch day {
case Sunday:
fmt.Println("Sunday")
case Monday:
fmt.Println("Monday")
case Tuesday:
fmt.Println("Tuesday")
case Wednesday:
fmt.Println("Wednesday")
case Thursday:
fmt.Println("Thursday")
case Friday:
fmt.Println("Friday")
case Saturday:
fmt.Println("Saturday")
default:
fmt.Println("Invalid Day")
}
}
Using the String() method is very useful as it allows to implement the printability of a string instead of creating an enum type.
Enum Skip Value
If you want to skip a value in the list of constants, you can use a blank identifier as shown:
const (
SundayDaysOfWeek = iota
Monday
_
Wednesday
Thursday
Friday
Saturday
)
Conclusion
This guide covers implementing and working with enumerators in the Go programming language. In addition, the Enum String Method and the Enum Skip Values were discussed with examples. We hope you found this article helpful. Check out other Linux Hint articles for more tips and tutorials.