Implementing Enums in Golang

An enum group related constants together in one type. Enums are a powerful feature with a wide range of uses. However, in Go, they’re implemented quite differently than most other programming languages.

`Photo by Christopher Gower on Unsplash

Note: Before you go any further, I expect you all have a beginner-level understanding of Go syntax and primitive types to understand the source-code.

In this article, I will show you how to implement enums in Golang using a predeclared identified iota. Before proceeding with enums, let’s first understand what is iota and how it is used.

What is iota?

iota is an identifier that is used with constant and which 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.

https://medium.com/media/d5fcdbcc3456cebb8c7e0a492b921cbd/href

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

https://medium.com/media/f0b3813510f84f921762c0afd9a207b8/href

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

https://medium.com/media/0b684ed55ac124ba8b394214e57f8e01/href

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

https://medium.com/media/eaedc2888705b80eff65134a22cebaca/href

Implement Enums using iota

I order to implement custom enum type we have to perform consider the following

  • Declare a new custom type — Integer Type.
  • Declare related constants — Using iota.
  • Creating common behavior — give the type a String function.
  • Creating additional behavior — give the type a EnumIndex function.

Example 1: Create an enum for the Weekdays.

Weekdays — Image By Author

https://medium.com/media/c8ed6bab85ffde07ba6517ae4cb0d80e/href

Example 2: Create an enum for the Directions.

Directions — Image By Author

https://medium.com/media/ad6baad27e069e3667b8d2346c40ad65/href

Conclusion

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. However, in Golang, they’re implemented quite differently than most other programming languages. Golang does not support enums directly. We can implement it using iota and constants.

That’s all for now… Keep Learning… Happy Learning ?


Implementing Enums in Golang was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.

An enum group related constants together in one type. Enums are a powerful feature with a wide range of uses. However, in Go, they’re implemented quite differently than most other programming languages.

`Photo by Christopher Gower on Unsplash

Note: Before you go any further, I expect you all have a beginner-level understanding of Go syntax and primitive types to understand the source-code.

In this article, I will show you how to implement enums in Golang using a predeclared identified iota. Before proceeding with enums, let’s first understand what is iota and how it is used.

What is iota?

iota is an identifier that is used with constant and which 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.

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

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

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

Implement Enums using iota

I order to implement custom enum type we have to perform consider the following

  • Declare a new custom type — Integer Type.
  • Declare related constants — Using iota.
  • Creating common behavior — give the type a String function.
  • Creating additional behavior — give the type a EnumIndex function.

Example 1: Create an enum for the Weekdays.

Weekdays — Image By Author

Example 2: Create an enum for the Directions.

Directions — Image By Author

Conclusion

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. However, in Golang, they’re implemented quite differently than most other programming languages. Golang does not support enums directly. We can implement it using iota and constants.

That's all for now… Keep Learning… Happy Learning ?


Implementing Enums in Golang was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.


Print Share Comment Cite Upload Translate
APA
Radhakishan Surwase | Sciencx (2024-03-28T22:20:24+00:00) » Implementing Enums in Golang. Retrieved from https://www.scien.cx/2021/03/15/implementing-enums-in-golang/.
MLA
" » Implementing Enums in Golang." Radhakishan Surwase | Sciencx - Monday March 15, 2021, https://www.scien.cx/2021/03/15/implementing-enums-in-golang/
HARVARD
Radhakishan Surwase | Sciencx Monday March 15, 2021 » Implementing Enums in Golang., viewed 2024-03-28T22:20:24+00:00,<https://www.scien.cx/2021/03/15/implementing-enums-in-golang/>
VANCOUVER
Radhakishan Surwase | Sciencx - » Implementing Enums in Golang. [Internet]. [Accessed 2024-03-28T22:20:24+00:00]. Available from: https://www.scien.cx/2021/03/15/implementing-enums-in-golang/
CHICAGO
" » Implementing Enums in Golang." Radhakishan Surwase | Sciencx - Accessed 2024-03-28T22:20:24+00:00. https://www.scien.cx/2021/03/15/implementing-enums-in-golang/
IEEE
" » Implementing Enums in Golang." Radhakishan Surwase | Sciencx [Online]. Available: https://www.scien.cx/2021/03/15/implementing-enums-in-golang/. [Accessed: 2024-03-28T22:20:24+00:00]
rf:citation
» Implementing Enums in Golang | Radhakishan Surwase | Sciencx | https://www.scien.cx/2021/03/15/implementing-enums-in-golang/ | 2024-03-28T22:20:24+00:00
https://github.com/addpipe/simple-recorderjs-demo