Switch Statement in Go



In this tutorial, we will learn about the switch statement in Go. We will cover the basics of using the switch statement, including how to handle multiple cases, use fallthrough, and switch on different types.


Understanding the Switch Statement in Go

A switch statement in Go is used to execute one block of code among many options. It is an alternative to using multiple if statements.


Basic Switch Statement

The basic syntax of a switch statement in Go is:

switch expression {
case value1:
    // statements
case value2:
    // statements
default:
    // default statements
}

Switch with Multiple Cases

Go allows multiple cases to be combined into a single case using commas.

switch expression {
case value1, value2:
    // statements
}

Switch with Fallthrough

The fallthrough statement can be used to continue execution to the next case.

switch expression {
case value1:
    // statements
    fallthrough
case value2:
    // statements
}

Type Switch

A type switch is used to compare types instead of values.

switch x.(type) {
case type1:
    // statements
case type2:
    // statements
}


Example 1: Basic Switch Statement

We can use a basic switch statement in Go to execute one block of code among multiple options.

For example,

  1. Create a variable named day and assign it a value.
  2. Use a switch statement to print the name of the day based on its value.

Go Program

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")
    }
}

Output

Wednesday


Example 2: Switch with Multiple Cases

We can use a switch statement with multiple cases combined into a single case.

For example,

  1. Create a variable named day and assign it a value.
  2. Use a switch statement to print the type of the day (weekend or weekday) based on its value.

Go Program

package main
import "fmt"
func main() {
    day := 6
    switch day {
    case 1, 2, 3, 4, 5:
        fmt.Println("Weekday")
    case 6, 7:
        fmt.Println("Weekend")
    default:
        fmt.Println("Invalid day")
    }
}

Output

Weekend


Example 3: Switch with Fallthrough

We can use the fallthrough statement in a switch case to continue execution to the next case.

For example,

  1. Create a variable named grade and assign it a value.
  2. Use a switch statement with fallthrough to print a message based on the grade.

Go Program

package main
import "fmt"
func main() {
    grade := "B"
    switch grade {
    case "A":
        fmt.Println("Excellent")
        fallthrough
    case "B":
        fmt.Println("Good")
        fallthrough
    case "C":
        fmt.Println("Average")
    default:
        fmt.Println("Invalid grade")
    }
}

Output

Good
Average


Example 4: Type Switch

We can use a type switch to compare types instead of values in Go.

For example,

  1. Create an empty interface variable named i and assign it a value of any type.
  2. Use a type switch to print the type of the variable.

Go Program

package main
import "fmt"
func main() {
    var i interface{} = 42
    switch v := i.(type) {
    case int:
        fmt.Printf("%T is an int\n", v)
    case float64:
        fmt.Printf("%T is a float64\n", v)
    case string:
        fmt.Printf("%T is a string\n", v)
    default:
        fmt.Printf("%T is an unknown type\n", v)
    }
}

Output

int is an int