In this post, we will see here how we can use the switch statement in the Golang. This allows you to conduct various actions in Golang based on various situations. If/else logic can also be expressed with a switch without an expression.
Golang also has a switch() statement, similar to those seen in other programming languages like PHP and Java.
The Golang switch statement is a way to write multiple if-else statements and is used to execute one of several code blocks based on the value of an expression.
The switch statement allows you to test multiple values of an expression and execute different code blocks depending on the result.
The Syntax:
switch expression { case exp1: statement1 case exp2: statement2 case exp3: statement3 ... default: statement4 }
Commas can be used to separate multiple expressions in the same case statement. We may also declare a default case, which will be used if none of the other cases match.
Go language supports two types of switch statements:
- Expression Switch: This switch statement is used to select one of many blocks of code to be executed based on the expression’s value.
- Type Switch: A type switch compares types instead of values. It will be compared to the type in the switch expression.
Table of Contents
Simple Example Using Switch Expression
The below example is using a switch expression.
package main import "fmt" import "time" func main() { switch time.Now().Weekday() { case time.Saturday, time.Sunday: fmt.Println("It's the weekend") default: fmt.Println("It's a weekday") } }
Output:
It's a weekday
Simple Example Using Switch Type
In the expression of the type-switch block, only interfaces can be used. The switch statement in Golang can also be used to test multiple values in a single case.
Let’s see the below example for the switch type:
package main import "fmt" func main() { var value interface{} switch q:= value.(type) { case bool: fmt.Println("value is of boolean type") case float64, float32: fmt.Println("value is of float type") case int: fmt.Println("value is of int type") default: fmt.Printf("value is of type: %T", q) } }
In this example, the value of type
is set to int, and the switch statement is used to test the value of type. The case float64, float32
tests for multiple values and will execute the code block if the value of type is either float64
or float
32. In this case, since the value of type is int, the code block for case int
will be executed, and the message “value is of int type” will be printed to the console.
Output:
value is of int type
Switch Statement With Expression
You can also use expressions in the switch statement in Golang. For example:
package main import "fmt" func main() { age := 34 switch { case age < 18: fmt.Println("younger") case age > 18: fmt.Println("older than 18") default: fmt.Println("age is not valid") } }
Conclusion
We have learned the basic usage of “switch” statement with examples. You can create simplifies complex decision-making using switch statement. We are comparing a variable against multiple alternatives using switch statement.