Iota

Go provides a special keyword called iota that makes it easy to define a sequence of related integer constants.

How it works

iota starts at 0 and increments by 1 for every constant defined in a const block. It resets to 0 whenever a new const block begins.

type emailStatus int

const (
    EmailBounced   emailStatus = iota // 0
    EmailInvalid                      // 1
    EmailDelivered                    // 2
    EmailOpened                       // 3
)

Benefits

  1. Less Repetition: You don’t have to manually assign 0, 1, 2... to every value.
  2. Readability: It clearly communicates that these values are part of a related sequence of states.
  3. Type Safety: Like string-based enums, using a custom type like emailStatus prevents you from accidentally passing a plain int to a function expecting a specific status.