Go Lesson 12 – Switch StatementS | Dataplexa

Switch Statements in Go

When a program needs to compare a value against many possible cases, using multiple if-else statements can become lengthy and hard to read.

Go provides the switch statement as a clean, powerful, and readable alternative for multi-branch decision making.


Basic Switch Statement

A switch statement compares a value against multiple cases and executes the matching block.

package main

import "fmt"

func main() {
    day := 3

    switch day {
    case 1:
        fmt.Println("Monday")
    case 2:
        fmt.Println("Tuesday")
    case 3:
        fmt.Println("Wednesday")
    default:
        fmt.Println("Invalid day")
    }
}

Only the matching case executes. If no case matches, the default block runs.


No Need for Break Statements

Unlike many languages, Go automatically breaks after a case executes. This prevents accidental fall-through bugs.

This makes switch statements safer and easier to maintain.


Switch with Multiple Values per Case

A single case can match multiple values. This is useful when different inputs should produce the same output.

package main

import "fmt"

func main() {
    month := 2

    switch month {
    case 12, 1, 2:
        fmt.Println("Winter")
    case 3, 4, 5:
        fmt.Println("Spring")
    case 6, 7, 8:
        fmt.Println("Summer")
    case 9, 10, 11:
        fmt.Println("Autumn")
    default:
        fmt.Println("Invalid month")
    }
}

This reduces repetition and improves readability.


Switch Without an Expression

Go allows switch statements without a value. Each case then acts like a conditional expression.

package main

import "fmt"

func main() {
    score := 78

    switch {
    case score >= 90:
        fmt.Println("Grade A")
    case score >= 75:
        fmt.Println("Grade B")
    case score >= 60:
        fmt.Println("Grade C")
    default:
        fmt.Println("Fail")
    }
}

This pattern often replaces complex if-else chains.


Fallthrough in Go

If you explicitly want execution to continue to the next case, use the fallthrough keyword.

package main

import "fmt"

func main() {
    level := 1

    switch level {
    case 1:
        fmt.Println("Basic access")
        fallthrough
    case 2:
        fmt.Println("Intermediate access")
    case 3:
        fmt.Println("Admin access")
    }
}

Use fallthrough carefully, as it ignores the next case condition.


Real-World Example: HTTP Status Codes

Switch statements are commonly used when handling known status values.

package main

import "fmt"

func main() {
    statusCode := 404

    switch statusCode {
    case 200:
        fmt.Println("Success")
    case 400:
        fmt.Println("Bad Request")
    case 401:
        fmt.Println("Unauthorized")
    case 404:
        fmt.Println("Not Found")
    case 500:
        fmt.Println("Server Error")
    default:
        fmt.Println("Unknown Status")
    }
}

This approach keeps API logic clean and easy to extend.


Best Practices for Switch Statements

  • Use switch for known discrete values
  • Prefer switch over long if-else chains
  • Always include a default case
  • Avoid unnecessary fallthrough usage

When to Use Switch vs If

  • Use if for complex logical conditions
  • Use switch for value-based decisions

What’s Next?

In the next lesson, you will learn about For Loops in Go, the only looping construct Go provides and a core part of Go programming.