Go

iota — Auto-Incrementing Constants (Enums)

admin by @admin ADMIN
Jun 14, 2026
May 31, 2026
Public
0 0 up · 0 down Sign in to vote
Go has no `enum` keyword but `iota` inside a const block gives you the same effect. Each line is the previous expression with iota auto-incremented.
Go
Raw
package main

import "fmt"

type Status int

const (
    StatusPending Status = iota   // 0
    StatusActive                  // 1
    StatusArchived                // 2
    StatusDeleted                 // 3
)

// Implementing Stringer makes %s print the friendly name
func (s Status) String() string {
    return [...]string{"pending", "active", "archived", "deleted"}[s]
}

// Bitflags — shift iota by 1 each time
type Perm int
const (
    Read    Perm = 1 << iota      // 1
    Write                         // 2
    Execute                       // 4
)

func main() {
    fmt.Println(StatusActive)                    // active
    fmt.Println(StatusPending + 1)               // active

    p := Read | Write
    fmt.Printf("%b can read: %t\n", p, p&Read != 0)   // 11 can read: true
}
Tags

Save your own code snippets

Create a free account and build your private vault. Share publicly whenever you want.