How to use Enums in Golang
Background
Imagine you’re building a Todo application using Go. Each task in your application has a status associated with it, indicating whether it’s “completed”, “archived” or “deleted”.
As a developer, you want to ensure that only valid status values are accepted when creating or updating tasks via your API endpoints. Additionally, you want to provide clear error messages to users if they attempt to set an invalid status apart from the valid ones.
Of course, you will say enums, right?
But the bad news is Go doesn’t provide enums and the good news is we can still implement it.
In this blog, we’ll explore how to effectively manage enums in Golang, including validation techniques to ensure data integrity and reliability.
Whether you are a beginner or an experienced developer, this guide aims to demystify enums and empower you to leverage their power in your Go projects.
What is Enum?
Enum is a short form of enumeration, which represents a distinct set of named constants. While languages like Java and C++ offer native support for enums, Go takes a different approach.
Instead of a dedicated enum type, Go developers use constants or custom types with iota to emulate enum-like behavior. Let’s begin!
Why Enums?
Enums make code more readable by providing descriptive names for values, rather than using raw integer or string constants. This enhances code clarity and makes it easier for other developers to understand and maintain the codebase.
Enums provide compile-time checking, which helps catch errors early in development. This prevents invalid or unexpected values from being assigned to variables, reducing the likelihood of runtime errors.
By restricting the possible values a variable can hold to a predefined set, enums help prevent logic errors caused by incorrect or unexpected values.
Defining Enums with Constants
Constants are a straightforward way to define enums in Go. Let’s consider a basic example where we define enum constants representing different days of the week.
package main
import "fmt"
const (
SUNDAY = "Sunday"
MONDAY = "Monday"
TUESDAY = "Tuesday"
WEDNESDAY = "Wednesday"
THURSDAY = "Thursday"
FRIDAY = "Friday"
SATURDAY = "Saturday"
)
func main() {
day := MONDAY
fmt.Println("Today is ", day) // "Today is Monday"
}
Simulating Enums with Custom Types and iota
While constants work well for simple enums, custom types with iota provide a more conversational approach in Go.
What is iota?iota
is a special identifier in Go that is used with const
declarations to generate a sequence of related values automatically.
It simplifies the process of defining sequential values, particularly when defining enums or declaring sets of constants with incrementing values.
When iota
is used in a const
declaration, it starts with the value 0 and increments by 1 for each subsequent occurrence within the same const
block. If iota
appears in multiple const
declarations within the same block, its value is reset to 0 for each new const
block.
package main
import "fmt"
const (
SUNDAY = iota // SUNDAY is assigned 0
MONDAY // MONDAY is assigned 1 (incremented from SUNDAY)
TUESDAY // TUESDAY is assigned 2 (incremented from MONDAY)
WEDNESDAY // WEDNESDAY is assigned 3 (incremented from TUESDAY)
THURSDAY // THURSDAY is assigned 4 (incremented from WEDNESDAY)
FRIDAY // FRIDAY is assigned 5 (incremented from THURSDAY)
SATURDAY // SATURDAY is assigned 6 (incremented from FRIDAY)
)
func main() {
fmt.Println(MONDAY, WEDNESDAY, FRIDAY) // Output: 1 3 5
}
Let’s rewrite the previous example using a custom type(int).
package main
import "fmt"
type Day int
const (
Sunday Day = iota
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
)
func main() {
day := Monday
fmt.Println("Today is ", day) // Output: Today is 1
}
In this example, we define a custom-type Day
and use iota
to auto-increment the values of the constants. This approach is more concise and offers better type safety.
Optionally, we can also use like iota + 1
, if we want our constants to start from 1.
To read the full version, please visit Canopas Blog.
Feedback and suggestions are most welcome, add them in the comments section.
Follow Canopas to get updates on interesting articles!
Keep exploring and innovating!!
Subscribe to my newsletter
Read articles from Canopas directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Canopas
Canopas
Unless you’re a Multimillion or a Billion dollar company, you probably don’t have a multimillion-dollar ad budget or professional Spinners. Your product needs to stand out on its own merits like App Quality, Performance, UI design, and User Experience. Most companies don't care about you, your product, and your vision or dreams. They don't give a damn about either their work helped you to get more business, revenue, users, or solving a problem. That's where CANOPAS comes into the picture. Whether you have a GREAT IDEA and you want to turn it into a DIGITAL PRODUCT. OR You need a team that can turn your NIGHTMARES into SWEET DREAMS again by improving your existing product. We help Entrepreneurs, startups, and small companies to bring their IDEA to LIFE by developing digital products for their business. We prefer using Agile and Scrum principles in project management for flexibility and rapid review cycles. We are not bound by technology. We will learn new technology if it significantly improves the performance of your app. We will solve your tech-related problems even though we are not THE EXPERT in it. And we've done it multiple times in the last 7 years. In the last seven years, we helped... A STARTUP to expand its users from 2500 to over 100000 by developing mobile apps for them. An enterprise client to redevelop their app that has 1M+ monthly paid users and 10M+ app downloads. Another enterprise client(5M+ app downloads in each store) to fix bugs and broken parts in the app and as a result, they had over 98% crash-free users. We offer a 100% MONEY BACK GUARANTEE if you don't like our work. No questions asked. Visit : https://canopas.com/blog