I will show you how to implement enumerators, or enums, in Golang using a predeclared identified iota. An enum is a data type consisting of a set of named constant values. Enums are a powerful feature with a wide range of uses.

What Is an Enum in Golang?

An enumerator, or enum, is a data type in Golang that consists of a set of named, constant values. While Golang doesn’t support enums, it can be implemented using the identifier iota with constants.

However, in Golang, they’re implemented quite differently than most other programming languages. Golang doesn’t support enums directly, however, we can implement it using iota and constants.

In order to implement enums in Golang, let’s first understand what iota is and how it is used.

 

What Is Iota in Golang?

iota is an identifier that is used with constant and can simplify constant definitions that use auto-increment numbers. The iota keyword represents integer constant starting from zero.

The iota keyword represents successive integer constants 0, 1, 2,…. It resets to 0 whenever the word const appears in the source code and increments after each const specification.

package main

import "fmt"

const (
	c0 = iota
	c1 = iota
	c2 = iota
)
func main() {
	fmt.Println(c0, c1, c2) //Print : 0 1 2
}

You can avoid writing successive iota in front of every constant. This can be simplified as in the below code listing:

package main

import "fmt"

const (
	c0 = iota
	c1
	c2
)

func main() {
	fmt.Println(c0, c1, c2) //Print : 0 1 2
}

To start a list of constants at 1 instead of 0, you can use iota in an arithmetic expression.

package main

import "fmt"

const (
	c0 = iota + 1
	c1
	c2
)

func main() {
	fmt.Println(c0, c1, c2) // Print : 1 2 3
}

You can use the blank identifier to skip a value in a list of constants.

package main

import "fmt"

const (
	c1 = iota + 1
	_
	c3
	c4
)

func main() {
	fmt.Println(c1, c3, c4) // Print : 1 3 4
}

More on Software EngineeringError Handling in React With Error Boundary: A Tutorial

 

How to Implement an Enum in Golang With Iota

In order to implement the custom enum type we have to perform, consider the following:

  1. Declare a new custom type: Integer type.
  2. Declare related constants: Use iota.
  3. Create a common behavior: Give the type a String function.
  4. Create additional behavior: Give the type an EnumIndex function.

 

Example 1: Create an Enum for the Days of the Week

Table with the week days created using enum in Golang.
Table with the days of the week created using enum in Golang. | Image: Radhakishan Surwase
package main

import "fmt"

// Weekday - Custom type to hold value for weekday ranging from 1-7
type Weekday int

// Declare related constants for each weekday starting with index 1
const (
	Sunday    Weekday = iota + 1 // EnumIndex = 1
	Monday                       // EnumIndex = 2
	Tuesday                      // EnumIndex = 3
	Wednesday                    // EnumIndex = 4
	Thursday                     // EnumIndex = 5
	Friday                       // EnumIndex = 6
	Saturday                     // EnumIndex = 7
)

// String - Creating common behavior - give the type a String function
func (w Weekday) String() string {
	return [...]string{"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}[w-1]
}

// EnumIndex - Creating common behavior - give the type a EnumIndex function
func (w Weekday) EnumIndex() int {
	return int(w)
}

func main() {
	var weekday = Sunday
	fmt.Println(weekday)             // Print : Sunday
	fmt.Println(weekday.String())    // Print : Sunday
	fmt.Println(weekday.EnumIndex()) // Print : 1
}
A tutorial on how to implement enums in Golang. | Video: ProgrammingKnowledge

More on Software Engineering7 Ways to Remove a Duplicate From a JavaScript Array

 

Example 2: Create an Enum for the Directions

table of the four cardinal directions created using enums in Golang.
Table of the four cardinal directions created using enums in Golang. | Image: Radhakishan Surwase
package main

import "fmt"

// Direction - Custom type to hold value for week day ranging from 1-4
type Direction int

// Declare related constants for each direction starting with index 1
const (
	North Direction = iota + 1 // EnumIndex = 1
	East                       // EnumIndex = 2
	South                      // EnumIndex = 3
	West                       // EnumIndex = 4
)

// String - Creating common behavior - give the type a String function
func (d Direction) String() string {
	return [...]string{"North", "East", "South", "West"}[d-1]
}

// EnumIndex - Creating common behavior - give the type a EnumIndex functio
func (d Direction) EnumIndex() int {
	return int(d)
}

func main() {
	var d Direction = West
	fmt.Println(d)             // Print : West
	fmt.Println(d.String())    // Print : West
	fmt.Println(d.EnumIndex()) // Print : 4
}
Expert Contributors

Built In’s expert contributor network publishes thoughtful, solutions-oriented stories written by innovative tech professionals. It is the tech industry’s definitive destination for sharing compelling, first-person accounts of problem-solving on the road to innovation.

Learn More

Great Companies Need Great People. That's Where We Come In.

Recruit With Us