Are you tired of manually managing a set of related constants in your Go programs? Say hello to enums! Though Go doesn’t natively support enumerations, there’s a smart workaround that will make your life easier. In this comprehensive guide, we’ll dive into the world of enums, helping you better organize and maintain your code. Let’s explore how to create and use enums in Go, along with best practices to ensure your code is clean and efficient.
This article is part of a series of guides designed to help you learn Go, and we’ll reference relevant articles throughout. So let’s get started and unlock the power of enums!
Introduction to enumerations
Enumerations, or enums, are a way to define a named set of related constants in programming languages. Enums make it easier to work with groups of values, like days of the week, status codes, or colors, making your code more readable and maintainable.
Imagine having a set of constants representing the days of the week. Instead of manually assigning integer values to each day, enums can provide a more elegant and organized solution.
Why Go doesn’t have built-in enum support
Go was designed to be a simple and efficient language, and its creators intentionally excluded some features found in other languages, like built-in enum support. However, Go provides a workaround using const
and iota
that achieves similar functionality to enums.
What is iota?

iota
is a predeclared identifier in Go, used for generating consecutive untyped integer constants. It starts at 0 and increments by 1 for each constant in a const
block. When a new const
block is encountered, iota
resets to 0. This behavior makes iota
a powerful tool for creating enums in Go.
How to create enums using const and iota
In Go, enums can be created using constant blocks (const
) combined with the iota
keyword. iota
is a predeclared identifier representing successive untyped integer constants. It resets to 0 whenever a new const
block is encountered and increments by 1 for each constant within the block.
Here’s an example of how to create an enum for the days of the week:
package main import "fmt" type DayOfWeek int const ( Sunday DayOfWeek = iota Monday Tuesday Wednesday Thursday Friday Saturday ) func main() { fmt.Println("Wednesday:", Wednesday) }
Practical examples of using enums in Go
Let’s explore some practical examples to see how enums can be useful in real-world scenarios.
Example 1: HTTP Status Codes
Enums can help make HTTP status code handling more readable:
type HTTPStatus int const ( OK HTTPStatus = 200 BadRequest = 400 Unauthorized = 401 Forbidden = 403 NotFound = 404 ) func handleRequest(status HTTPStatus) { switch status { case OK: fmt.Println("Request successful") case BadRequest: fmt.Println("Bad request") case Unauthorized: fmt.Println("Unauthorized access") case Forbidden: fmt.Println("Access forbidden") case NotFound: fmt.Println("Resource not found") default: fmt.Println("Unknown status") } }
Example 2: Colors
type Color int const ( Red Color = iota Green Blue Yellow Magenta Cyan White Black ) func printColorName(color Color) { switch color { case Red: fmt.Println("Red") case Green: fmt.Println("Green") case Blue: fmt.Println("Blue") case Yellow: fmt.Println("Yellow") case Magenta: fmt.Println("Magenta") case Cyan: fmt.Println("Cyan") case White: fmt.Println("White") case Black: fmt.Println("Black") default: fmt.Println("Unknown color") } } func main() { printColorName(Yellow) // Output: Yellow }
This example demonstrates how to create and use an enum for different colors. The printColorName
function takes a Color
enum value and prints its name.
String Methods to Enum Types
Sometimes, it’s useful to represent enum values as strings for better readability or logging purposes. You can add a string method to your custom enum type, like this:
package main import "fmt" type Color int const ( Red Color = iota Green Blue Yellow Magenta Cyan White Black ) func (c Color) String() string { return [...]string{"Red", "Green", "Blue", "Yellow", "Magenta", "Cyan", "White", "Black"}[c] } func main() { fmt.Println("Yellow:", Yellow) // Output: Yellow: Yellow }
The String()
method returns the string representation of a Color
enum value.
Enum Default Values
As enums in Go are essentially integers, they have a default value of 0. By convention, the first value in an enum should be 0 to align with Go’s default zero-value behavior for uninitialized variables.
type StatusCode int const ( Undefined StatusCode = iota OK BadRequest Unauthorized )
In this example, the Undefined
enum value is set to 0 by default, and other values increment by 1.
Best practices for working with enums in Go

Here are some best practices to follow when working with enums in Go:
- Use custom types: Define a custom type for your enum to ensure type safety and improve readability.
- Use constants: Utilize
const
blocks withiota
to define a sequence of enum values. - Use meaningful names: Choose descriptive names for your enum values to make your code easier to understand.
- Document your enums: Add comments to describe the purpose and usage of your enums, especially if they represent complex or domain-specific concepts.
- Start enums with a zero value: By convention, the first value in an enum should be 0. This aligns with Go’s default zero-value behavior for uninitialized variables.
Using Enums in Practice
Enums are a powerful way to manage sets of related constants, making your code more organized, readable, and maintainable. Many popular libraries and frameworks utilize enums for various purposes. Here are a few use cases where enums can be beneficial, along with real-life examples from libraries:
- Representing days of the week, months, or seasons: The
time
package in Go’s standard library uses integer constants to represent days of the week and months, withiota
to define their values. You can find this in the time package documentation. - Managing status codes (HTTP, error codes, etc.): The
net/http
package in Go’s standard library uses integer constants to represent HTTP status codes. Check out the net/http package documentation for details. - Defining application states or modes: The
golang.org/x/crypto/ssh
package, an SSH library for Go, uses enums for representing various states of the SSH protocol, such aspacketType
,channelType
, and more. See the golang.org/x/crypto/ssh package documentation for examples. - Grouping colors, materials, or other properties: The
github.com/fogleman/gg
package, a 2D graphics library for Go, uses enums for representing different line cap styles and join styles. Explore the github.com/fogleman/gg package documentation for more information.
Frequently Asked Questions
Conclusion
In conclusion, emulating enumerations in Go using const
and iota
is a handy technique for managing a set of related constants, improving the readability and maintainability of your code. Now that you’ve mastered enums in Go, we encourage you to explore other topics in our series of Golang articles:
- How to work with Go flow structures: for, if, switch
- A Full guide to Golang Arrays, Slices, and Maps
- Working with Functions in Golang: A Ultimate Guide
- Generics in Golang: Simple and Complete View
By following these guides, you’ll have a strong foundation in Go, allowing you to tackle a wide range of projects with confidence. Happy coding!