0Pricing
Go Academy · Lesson

switch Statements in Go

Tagged and tagless switch, fallthrough

switch Statements in Go is a free Go Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Go Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

switch Basics

Go's switch is more powerful than in C: no fall-through by default, and cases can have multiple values:

day := "Monday"
switch day {
case "Saturday", "Sunday":
    fmt.Println("Weekend")
default:
    fmt.Println("Weekday")
}

No Implicit Fallthrough

In Go, each case body breaks automatically — unlike C where you must write break explicitly:

  • Cleaner code, fewer bugs
  • Use fallthrough explicitly when you need C-like behavior (rare)

switch with Init Statement

Like if, switch accepts an optional init statement:

switch os := runtime.GOOS; os {
case "linux":
    fmt.Println("Linux")
case "darwin":
    fmt.Println("macOS")
default:
    fmt.Printf("Other: %s\n", os)
}

Tagless switch (if-else chain alternative)

A tagless switch has no expression after the keyword — each case evaluates a boolean:

score := 75
switch {
case score >= 90:
    fmt.Println("A")
case score >= 80:
    fmt.Println("B")
case score >= 70:
    fmt.Println("C")
default:
    fmt.Println("F")
}

Type Switch

A type switch inspects the dynamic type of an interface value:

func describe(i interface{}) {
    switch v := i.(type) {
    case int:
        fmt.Printf("int: %d\n", v)
    case string:
        fmt.Printf("string: %s\n", v)
    default:
        fmt.Printf("unknown: %T\n", v)
    }
}

fallthrough

Use fallthrough to explicitly execute the next case body regardless of its condition (rarely needed):

n := 1
switch n {
case 1:
    fmt.Println("one")
    fallthrough
case 2:
    fmt.Println("one or two")
}

Multiple Values per Case

List multiple values separated by commas in a single case:

func isVowel(c byte) bool {
    switch c {
    case 'a', 'e', 'i', 'o', 'u':
        return true
    }
    return false
}

switch vs if-else

Use switch when:

  • You are comparing one expression against many values
  • You have a type switch for interface dispatch
  • Readability improves over a chain of if-else

Use if-else when conditions are different boolean expressions.

break in switch

To exit a switch early, use break. To exit an outer loop from within a switch, use a labeled break:

loop:
for {
    switch input() {
    case "quit":
        break loop  // exits the for loop
    }
}

Default Case

The default case runs when no other case matches. Best practices:

  • Always include default when handling enumerations — to catch unexpected values
  • Position it at the bottom for readability
  • In type switches, use default to handle unsupported types gracefully

Quick Check

What happens in Go when a switch case matches — does execution fall through?

Recap: switch in Go

Go's switch is clean and powerful:

  • No implicit fallthrough — use fallthrough explicitly
  • Init statement for scoping temporaries
  • Tagless switch as an if-else chain alternative
  • Type switch for interface dispatch
  • Multiple values per case with comma separation

Next: loop control with break, continue, and labels.

Frequently asked questions

Is the “switch Statements in Go” lesson free?

Yes — the full text of “switch Statements in Go” is free to read here on the web, and the Go Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Go Academy course, upgrade to CoddyKit PRO.

What will I learn in “switch Statements in Go”?

Tagged and tagless switch, fallthrough You practise Go Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Go Academy?

No prior experience is required. Go Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “switch Statements in Go” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Go Academy lesson?

Yes. Every Go Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. if Statements and Init Expressions
  2. The for Loop: All Patterns
  3. switch Statements in Go
  4. break, continue and Labels
← Back to Go Academy