Duplicate Case in Switch

duplicate case 1 in switch previous case at line 5

Quick Answer

You have the same value in two case clauses. Remove the duplicate or merge the case bodies.

Why This Happens

Go does not allow duplicate case values in switch statements because it would be ambiguous which case to execute. If you need two values to have the same behavior, list them in the same case clause separated by commas.

The Problem

switch x {
case 1:
    fmt.Println("one")
case 1: // duplicate case
    fmt.Println("also one")
}

The Fix

switch x {
case 1:
    fmt.Println("one")
case 2:
    fmt.Println("two")
}

Step-by-Step Fix

  1. 1

    Identify the duplicate

    The compiler tells you the duplicate value and the line of the first occurrence.

  2. 2

    Remove or merge

    Remove the duplicate case or combine the two cases into one: case 1, 2:.

  3. 3

    Review switch logic

    Ensure the remaining cases correctly handle all expected values.

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