Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Switch Statements in Go Programming

Introduction

The switch statement in Go provides a convenient way to select one of many code blocks to be executed. It is an alternative to using multiple if statements and makes the code more readable and maintainable.

Basic Syntax

The basic syntax of a switch statement in Go is as follows:

switch expression {
case value1:
    // code to execute if expression equals value1
case value2:
    // code to execute if expression equals value2
default:
    // code to execute if expression does not match any case
}
                

Let's break down this syntax:

  • switch expression: The value or expression to be evaluated.
  • case value: The cases to be matched against the switch expression. If a match is found, the corresponding code block is executed.
  • default: (Optional) The default case that is executed if no other cases match.

Example 1: Basic Switch

Here is a basic example of a switch statement in Go:

package main

import "fmt"

func main() {
    day := "Tuesday"
    
    switch day {
    case "Monday":
        fmt.Println("Start of the work week.")
    case "Tuesday":
        fmt.Println("Second day of the work week.")
    case "Wednesday":
        fmt.Println("Midweek day.")
    case "Thursday":
        fmt.Println("Almost the end of the work week.")
    case "Friday":
        fmt.Println("End of the work week.")
    default:
        fmt.Println("Weekend!")
    }
}
                

In this example, the day variable is evaluated in the switch statement. The output will be:

Second day of the work week.

Example 2: Switch with Multiple Cases

A switch statement can also have multiple cases with the same code block:

package main

import "fmt"

func main() {
    day := "Saturday"
    
    switch day {
    case "Monday", "Tuesday", "Wednesday", "Thursday", "Friday":
        fmt.Println("It's a weekday.")
    case "Saturday", "Sunday":
        fmt.Println("It's the weekend.")
    default:
        fmt.Println("Not a valid day.")
    }
}
                

In this example, if day is either "Saturday" or "Sunday", the output will be:

It's the weekend.

Example 3: Switch Without Expression

In Go, you can also use a switch statement without an expression. This allows you to use switch as a cleaner alternative to a series of if...else if statements:

package main

import "fmt"

func main() {
    number := 9
    
    switch {
    case number < 0:
        fmt.Println("Number is negative.")
    case number == 0:
        fmt.Println("Number is zero.")
    case number > 0:
        fmt.Println("Number is positive.")
    }
}
                

In this example, the switch statement checks the conditions directly. The output will be:

Number is positive.

Conclusion

The switch statement in Go is a powerful control structure that can simplify your code by replacing multiple if statements. It improves readability and maintainability, making it easier to understand the flow of the program.