Go Multi-case switch statement

Gayathri. B
 

Multi‑Case switch in Go

A switch can match multiple values in the same case. This is handy when different input values should trigger the exact same logic.

Syntax

switch expression {
case x, y:
    // runs if expression == x OR expression == y
case v, w:
    // runs if expression == v OR expression == w
case z:
    // runs if expression == z
default:
    // optional fallback when no case matches
}

Example: Classifying Weekdays

The snippet below groups weekday numbers into odd, even, and weekend categories.

package main

import "fmt"

func main() {
    day := 5

    switch day {
    case 1, 3, 5:
        fmt.Println("Odd weekday")
    case 2, 4:
        fmt.Println("Even weekday")
    case 6, 7:
        fmt.Println("Weekend")
    default:
        fmt.Println("Invalid day number")
    }
}

Output when day := 5:

Odd weekday


Tags
Our website uses cookies to enhance your experience. Learn More
Accept !

GocourseAI

close
send