golang

Golang Enum

An enum, short for enumerator, is a special data used to declare a set of named constant variables. Using enums, we can create a set of complex constants with descriptive variable names and unique values.

Let us discuss how to work with enumerators in Go.

Golang Enum

The syntax for declaring an enum in Go is as shown:

type enum_namedata_type
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:

const (
    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:

const (
    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:

// one
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:

fmt.Println(Sunday == driver)

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:

package main
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:

package main
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:

package main
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:

Day ofthe week:  4

Enum String Method

Suppose we want to add a more descriptive message. We can add a method for the DaysOfWeek type as shown:

package main
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:

type DaysOfWeek int64

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.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list