Golang Program to Create an enum class


An enum combines related constants into a single type. Enums are a strong feature with many applications. However, compared to the majority of other programming languages, they are implemented very differently in Go. In this article, we'll see how to use a predeclared identifiable iota to implement enums in Golang.

IOTA − Iota is an identifier that is used with constants and can make auto-increment number-based constant definitions simpler. The keyword "iota" stands for an integer constant that begins at zero.

Implementing Iota

package main
import "fmt"
const (
   c0 = iota + 1
   c1
   c2
)
func main() {
   fmt.Println(c0, c1, c2) // Print : 1 2 3
}

Output

1 2 3

Creating an Enum for Weekdays

Algorithm

Step 1 − Import the fmt package that allows us to print anything on the screen.

Step 2 − create a new data type as Weekday to store the integer types for weekdays.

Step 3 − Declare related constants for each weekday starting with index 1.

Step 4 − Create a function to get the weekday as a string from the integer values provided.

Step 5 − creating a function to get the index of the enum.

Step 6 − calling the main() function.

Step 7 − initializing a Weekday type and storing a day in it.

Step 8 − print the corresponding weekday and index by calling the getWeekday() and getIndex() function respectively.

Example

In the following example, we are creating enum for weekdays.

package main
import "fmt"
type Weekday int

// Declaring related constants for each weekday starting with index 1
const (
   Sunday Weekday = iota + 1
   Monday
   Tuesday
   Wednesday
   Thursday
   Friday
   Saturday
)

// creating a function to get the weekday as string from the integer values provided
func (w Weekday) getWeekday() string{
   return [...]string{"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
   "Saturday"} [w-1]
}

// creating a function to get the index of the enum
func (w Weekday) getIndex() int {
   return int(w)
}
func main() {
   var weekday = Sunday
   fmt.Println(weekday)
   fmt.Println(weekday.getWeekday())
   fmt.Println(weekday.getIndex())
}

Output

1
Sunday
1

Creating an Enum for Direction

Algorithm

STEP 1 − Import the fmt package that allows us to print anything.

STEP 2 − Create a new type Direction to store the integer types for direction starting from 1-7

STEP 3 − Declare constants for each direction as Direction and index the set of constants with iota. Note that the first direction i.e north is indexed as iota + 1 i.e 1 and the following directions will get the index as auto incremented values.

STEP 4 − Now we need to create functions to the Direction type. The first function returns the direction as the string and we have named this function as getDirection(). While the other function returns the integer value of enum index and we have named this function as getIndex().

STEP 5 − Now call the main() function this is the main starting point of the program from where the program gets executed.

STEP 6 − Creating a new variable of Direction type and storing one of the direction as value to it.

STEP 7 − Now we can print the direction as well as enum index corresponding to that direction by calling the getDirection() and getIndex() function respectively.

STEP 8 − Print the result returned by this function on the screen by using fmt.Println() function.

Example

In the following example we are creating an enum for direction

package main
import "fmt"

// creating a new custom type Direction to store the direction values as integers from 1-4
type Direction int

// Declaring related constants for each direction starting with index 1
const (
   North Direction = iota + 1
   East
   South
   West
)

// creating a function to get the directions as string from the integer values provided
func (d Direction) getDirection() string {
   return [...]string{"North", "East", "South", "West"}[d-1]
}
func (d Direction) getIndex() int {
   return int(d)
}
func main() {
   var d Direction = West
   fmt.Println(d)
   fmt.Println(d.getDirection())
   fmt.Println(d.getIndex())
}

Output

4
West
4

Conclusion

We have successfully compiled and executed a golang program to create an enum class along with examples. we have created various functions to implement the logic of creating the enum class.

Updated on: 10-Feb-2023

344 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements