In this tutorial you will learn about the Go Switch Statement and its application with practical example.
Go Switch Statement
The switch statement is simplified form of the nested if … else if statement , it avoids long chain of if..else if..else statements. A switch statement evaluates an expression against multiple cases in order to identify the block of code to be executed.
Syntax:-
1 2 3 4 5 6 7 8 9 10 11 |
switch <expression>{ case <val1>: //Block of code to be executed if expression = val1; case <val2>: //Block of code to be executed if expression = val2; default: //Optional //Block of code to be executed //if expression is different //from both val1 and val2; } |
Example:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
package main import "fmt" func main() { var dayOfWeek = 5 fmt.Println("W3Adda - Go switch statement.") switch dayOfWeek { case 1: fmt.Println("Today is Monday.") case 2: fmt.Println("Today is Tuesday.") case 3: fmt.Println("Today is Wednesday.") case 4: fmt.Println("Today is Thursday.") case 5: fmt.Println("Today is Friday.") case 6: fmt.Println("Today is Saturday.") case 7: fmt.Println("Today is Sunday.") default: fmt.Println("Invalid Weekday.") } } |
Output:-
In Go, you can have multiple values in a single case statement separated with comma (,) as following –
Example :-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
package main import "fmt" func main() { var dayOfWeek = 5 fmt.Println("W3Adda - Go switch with multiple case combnied.") switch dayOfWeek { case 1, 2, 3, 4, 5: fmt.Println("It's a Weekday.") case 6, 7: fmt.Println("Its Weekend.") default: fmt.Println("Invalid Day") } } |
Output:-
Go Switch fallthrough
In switch statement a fallthrough statement allows to execute all the following statements after a match found.
Example:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
package main import "fmt" func main() { var x = 2 fmt.Println("W3Adda - Go switch with fallthrough.") switch x { case 1: fmt.Println("1") fallthrough case 2: fmt.Println("2") fallthrough case 3: fmt.Println("3") } } |
Output:-
Go Switch with a short statement
In Go, a switch statement can also contain a short declaration statement like the If statement before the conditional expression as following –
Example:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
package main import "fmt" func main() { fmt.Println("W3Adda - Go switch with short statement.") switch dayOfWeek := 5; dayOfWeek { case 1, 2, 3, 4, 5: fmt.Println("It's a Weekday.") case 6, 7: fmt.Println("Its Weekend.") default: fmt.Println("Invalid Day") } } |
Output:-
Go switch with no expression
Syntax:-
1 2 3 4 5 6 |
switch { case <boolean_expression_2>: //do something case <boolean_expression_2>: //do something } |
Example:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package main import "fmt" func main() { var marks = 55 fmt.Println("W3Adda - Go switch with no expression.") switch { case marks > 60: fmt.Println("Passed with 'A' Grade") case marks < 60 && marks >=50: fmt.Println("Passed with 'B' Grade") case marks < 50 && marks >= 35: fmt.Println("Passed with 'C' Grade") default: fmt.Println("You're Fail") } } |
Output:-
Note:- In Go, a break statement can be used to inside your matched case to exit the switch statement.