Iota Resets in Each Const Block

unexpected constant value: iota resets to 0 in new const block

Quick Answer

iota resets to 0 at the beginning of each const block. Keep related constants in the same const block to maintain sequential values.

Why This Happens

Go's iota identifier increments by one for each constant in a const block and resets to 0 at the start of a new const block. If you split related constants into separate blocks, the numbering restarts, which can cause duplicate values and logic errors.

The Problem

const (
    StatusPending = iota // 0
    StatusActive         // 1
)

const (
    StatusClosed = iota // 0 again, not 2!
)

The Fix

const (
    StatusPending = iota // 0
    StatusActive         // 1
    StatusClosed         // 2
)

Step-by-Step Fix

  1. 1

    Identify separate const blocks

    Find related iota constants that are split across multiple const blocks.

  2. 2

    Merge into one block

    Combine all related constants into a single const block.

  3. 3

    Verify values

    Print or test the constant values to confirm they are correct.

Bugsly catches this automatically

Bugsly's AI analyzes this error pattern in real-time, explains what went wrong in plain English, and suggests the exact fix — before your users even report it.

Try Bugsly free