How and Why to Write Enums in Go
Tags: go, backend, enum, constant
Published: 2021-04-20 13:18:22

---
title: How and Why to Write Enums in Go
published: true
date: 2021-04-19 13:24:38 UTC
tags: Golang,Go,backend,enum,constant
canonical_url: https://qvault.io/golang/golang-enum/
---

 ![list with pencil and paper](https://qvault.io/wp-content/uploads/2021/04/list-with-pencil-and-paper.jpg "list with pencil and paper")

The post [How and Why to Write Enums in Go](https://qvault.io/golang/golang-enum/) first appeared on [Qvault](https://qvault.io).

An **enum** , which is short for **enumerator** , is a set of named constant values. Enums are a powerful tool that allow developers to create complex sets of constants that have useful names yet simple and unique values.

## Syntax Example

Within a constant declaration, the `iota` keyword denotes successive untyped integer constants.

```
type BodyPart int

const (
    Head BodyPart = iota // Head = 0
    Shoulder // Shoulder = 1
    Knee // Knee = 2
    Toe // Toe = 3
)
```

## Why should you use enums?

### Why would you want an integer constant called `Head` with a value of `0`? And if you did, couldn’t you just use `const Head = 0`?

Yes, you _could_ do that, but enums are powerful by how they group sets of constants and guarantee unique values. By using an enum, you’re _ensured by the compiler_ that none of the constants in your group (`Head`, `Shoulder`, `Knee`, and `Toe`) have the same value.

### Why not just use strings? `const Head = "head"` and `const Shoulder = "shoulder"`?

Besides the overlapping answer of the compiler not guaranteeing uniqueness, a string can also be overkill. If you have a group of 4, 10, or even 100 unique values, do you really need to store an entire `string`? A small `int` will take up less room in your program’s memory space.

It’s not just about the space though, especially with how powerful modern hardware is. Let’s say you had some configuration variables like in the following example.

```
const (
    statusSuccess = iota
    statusFailed
    statusPending
    statusComplete
)
```

Now pretend you need to change the name of `statusFailed` to `statusCancelled`, perhaps to become consistent with the rest of the codebase. If you had previously used the string value `failed` instead of an enum, and now that value is strewn all across various databases and files in the system, it becomes _really_ hard to change it. If you had just used an `enum`, you can change the name without confusing anyone.

## Enums starting from 1

Sometimes if your a massochist, or perhaps a Lua developer, you’ll want your list of enums to start with a value of `1` instead of the default `0`.

```
const (
    Head = iota + 1 // 1
    Shoulder // 2
    Knee // 3
    Toe // 4
)
```

## Enums with multipliers

The `iota` keyword simply represents an incrementing interger constant that’s one number larger each time it’s used within a `const` block. You can use it to do whatever math you like.

```
const (
    Head = iota + 1 // 0 + 1 = 1
    Shoulder = iota + 2 // 1 + 2 = 3
    Knee = iota * 10 // 2 * 10 = 20
    Toe = iota * 100 // 3 * 100 = 300
)
```

With that in mind, remember that just because you _can_ doesn’t mean you _should_.

## Enums that skip values

If you want to skip a value just use the `_` character like you do to ignore return variables.

```
const (
    Head = iota // 0
    _
    Knee // 1
    Toe // 2
)
```

## String Enums in Go

Go doesn’t have any built-in `string` functionality for enums, but it’s pretty easy to simply implement a clean `String()` method. By using a `String()` method instead of setting the constants themselves as string types, you can get the same benefits of an enum with the “printability” of a string.

```
type BodyPart int

const (
    Head BodyPart = iota // Head = 0
    Shoulder // Shoulder = 1
    Knee // Knee = 2
    Toe // Toe = 3
)

func (bp BodyPart) String() string {
    return [...]string{"Head", "Shoulder", "Knee", "Toe"}
}
```

There are some “gotchas” to this approach however, so be careful. If the number of declarations in your `const` block is different than the number of entries in the “[constant slice](https://qvault.io/golang/golang-constant-maps-slices/)” created by your `String()` method, the compiler won’t alert you to the potential “out of bounds” error. Also, if you ever update the name of one of the constants don’t forget to update it’s corresponding string in the list.

Thanks for reading, now take a course!

Interested in a high-paying job in tech? Land interviews and pass them with flying colors after taking my hands-on coding courses.

[Start coding now](https://qvault.io)

### Questions?

Follow and hit me up on Twitter [@q\_vault](https://twitter.com/q_vault) if you have any questions or comments. If I’ve made a mistake in the article be sure to [let me know](https://qvault.io/contact/) so I can get it corrected!

[Subscribe](https://mailchi.mp/5c7f5c281bbe/qvault-newsletter-subscribe) to my newsletter for more coding articles delivered straight to your inbox.