In this Tutorial -:
Introduction
Switch statements are powerful control structures that allow developers to execute different blocks of code based on the value of an expression. In Go programming, the switch statement provides an elegant and concise way to handle multiple conditions efficiently.
In this tutorial, we’ll explore the switch statement in Go, understand its syntax, and learn how to leverage its features to make your code more readable and maintainable.
Prerequisites
Before we begin, ensure you have Go installed on your system. If you haven’t already done so, you can download the latest version of Go from the official website: https://golang.org/dl/ Once installed, verify the installation by opening a terminal or command prompt and running the following command:
go version
If Go is installed correctly, it will display the version number.
The Switch Statement in Go
The basic syntax of the switch statement in Go is as follows:
switch expression {
case value1:
// Code to be executed if expression equals value1
case value2:
// Code to be executed if expression equals value2
...
default:
// Code to be executed if expression doesn't match any case
}
Here’s an example of using a switch statement in Go:
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")
case 4:
fmt.Println("Thursday")
case 5:
fmt.Println("Friday")
case 6:
fmt.Println("Saturday")
case 7:
fmt.Println("Sunday")
default:
fmt.Println("Invalid day")
}
}
O/P
Wednesday
In this example, the switch statement checks the value of day and prints the corresponding day of the week.
Using Fallthrough
In Go, the fallthrough keyword allows you to transfer control to the next case after executing the current one, even if the next case’s condition is not true. Here’s an example:
package main
import "fmt"
func main() {
num := 3
switch num {
case 1:
fmt.Println("One")
case 2:
fmt.Println("Two")
case 3:
fmt.Println("Three")
fallthrough
default:
fmt.Println("Other")
}
}
In this example, when num is 3, “Three” will be printed, and then control will fall through to the next case, printing “Other” as well.
Using Switch Without an Expression
Go allows you to use the switch statement without an expression, making it an alternative to if-else chains. Here’s an example:
package main
import "fmt"
func main() {
age := 30
switch {
case age < 18:
fmt.Println("You are a minor.")
case age >= 18 && age < 65:
fmt.Println("You are an adult.")
default:
fmt.Println("You are a senior citizen.")
}
}
In this example, we use a switch statement without an expression to check different age ranges.
The switch statement is a versatile and flexible tool in Go programming, enabling you to handle multiple conditions effectively. In this tutorial, we explored the syntax and usage of the switch statement, including fallthrough and using it without an expression. By mastering switch statements, you can improve the readability and maintainability of your Go code and handle complex decision-making scenarios with ease.