Go Switch Statement
[ Go Language Conditional Statements](#)
The switch statement is used to execute different actions based on different conditions. Each case branch is unique, and they are tested from top to bottom until a match is found.
The execution process of a switch statement proceeds from top to bottom until a matching case is found. There is no need to add a break after the matching case.
By default, a case in a switch statement automatically includes a break statement. Once a match is successful, other cases will not be executed. If we need to execute the subsequent cases, we can use `fallthrough`.
### Syntax
The syntax for the switch statement in the Go programming language is as follows:
```go
switch var1 {
case val1:
...
case val2:
...
default:
...
}
The variable `var1` can be of any type, while `val1` and `val2` can be values of the same type. The type is not limited to constants or integers, but must be the same type; or an expression that ultimately results in the same type.
You can test multiple possible values that meet the conditions at the same time by separating them with commas, for example: `case val1, val2, val3`.
Flowchart:
!(#)
## Example
```go
package main
import "fmt"
func main() {
/* Define local variables */
var grade string = "B"
var marks int = 90
switch marks {
case 90:
grade = "A"
case 80:
grade = "B"
case 50, 60, 70:
grade = "C"
default:
grade = "D"
}
switch {
case grade == "A":
fmt.Printf("Excellent!n")
case grade == "B", grade == "C":
fmt.Printf("Goodn")
case grade == "D":
fmt.Printf("Passn")
case grade == "F":
fmt.Printf("Failn")
default:
fmt.Printf("Poorn");
}
fmt.Printf("Your grade is %sn", grade );
}
The execution result of the above code is:
Excellent! Your grade is A
* * *
## Type Switch
The switch statement can also be used as a type-switch to determine the actual type of a variable stored in an interface.
The syntax format for Type Switch is as follows:
```go
switch x.(type) {
case type:
statement(s);
case type:
statement(s);
/* You can define any number of cases */
default: /* Optional */
statement(s);
}
## Example
```go
package main
import "fmt"
func main() {
var x interface{}
switch i := x.(type) {
case nil:
fmt.Printf(" x's type :%T", i)
case int:
fmt.Printf("x is int")
case float64:
fmt.Printf("x is float64")
case func(int) float64:
fmt.Printf("x is func(int)")
case bool, string:
fmt.Printf("x is bool or string")
default:
fmt.Printf("unknown type")
}
}
The execution result of the above code is:
x's type :
### fallthrough
Using `fallthrough` forces the execution of the subsequent case statement. `fallthrough` does not check whether the expression result of the next case is true.
## Example
```go
package main
import "fmt"
func main() {
switch {
case false:
fmt.Println("1γcase condition is false")
fallthrough
case true:
fmt.Println("2γcase condition is true")
fallthrough
case false:
fmt.Println("3γcase condition is false")
fallthrough
case true:
fmt.Println("4γcase condition is true")
case false:
fmt.Println("5γcase condition is false")
fallthrough
default:
fmt.Println("6γdefault case")
}
}
The execution result of the above code is:
2γcase condition is true
3γcase condition is false
4γcase condition is true
From the output of the above code, it can be seen that: the switch starts executing from the first case whose expression is true. If a case has `fallthrough`, the program will continue to execute the next case, and it will not check whether the expression of the next case is true.
[ Go Language Conditional Statements](#)
YouTip