Go Switch Statement

Introduction

The switch statement in Go is a powerful control structure that allows you to execute one block of code among many based on the value of an expression. It is often more readable and efficient than multiple if-else statements when dealing with multiple conditions. In this chapter, you will learn the syntax and usage of the switch statement in Go, with examples to illustrate different scenarios.

Switch Statement Syntax

Basic Switch Statement

A basic switch statement evaluates an expression and executes the case that matches the value of the expression.

Syntax:

switch expression {
case value1:
    // code to execute if expression == value1
case value2:
    // code to execute if expression == value2
default:
    // code to execute if no case matches
}

Example:

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") // Output: Wednesday
    case 4:
        fmt.Println("Thursday")
    case 5:
        fmt.Println("Friday")
    default:
        fmt.Println("Weekend")
    }
}

Switch with Multiple Cases

You can group multiple cases together if they should execute the same block of code.

Example:

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") // Output: Weekend
    default:
        fmt.Println("Invalid day")
    }
}

Switch with Expressions

You can use expressions in case statements to handle more complex conditions.

Example:

package main

import "fmt"

func main() {
    age := 25

    switch {
    case age < 18:
        fmt.Println("Minor")
    case age >= 18 && age <= 65:
        fmt.Println("Adult") // Output: Adult
    default:
        fmt.Println("Senior")
    }
}

Switch with Fallthrough

By default, Go switch statements do not fall through to the next case. However, you can explicitly use the fallthrough keyword to continue executing the next case.

Example:

package main

import "fmt"

func main() {
    num := 2

    switch num {
    case 1:
        fmt.Println("One")
    case 2:
        fmt.Println("Two") // Output: Two
        fallthrough
    case 3:
        fmt.Println("Three") // Output: Three
    default:
        fmt.Println("Other")
    }
}

Switch on Types (Type Switch)

A type switch is used to compare types of interface values. It is useful when dealing with values of different types stored in an interface.

Example:

package main

import "fmt"

func main() {
    var x interface{} = 10

    switch v := x.(type) {
    case int:
        fmt.Println("x is an int") // Output: x is an int
    case string:
        fmt.Println("x is a string")
    default:
        fmt.Println("Unknown type")
    }
}

Conclusion

The switch statement in Go provides a clean and efficient way to handle multiple conditions. By understanding how to use basic switch, multiple cases, expressions in case statements, fallthrough, and type switches, you can write more readable and maintainable code. The switch statement is used for controlling the flow of your program based on the value of an expression or the type of a variable.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top