Go language switch statement
switch
statement is used to perform different actions based on different conditions, each case
branches are unique and tested one by one from top to bottom until they match.
switch
statement is executed from top to bottom until a match is found, and there is no need to add any more after the match break
.
By default, the case
statement comes with the break
statement at the end of the switch
, after successful matching, no other case
will be executed, the other will not be executed after the match is successful case
if we need to execute the following case
, you can use the fallthrough
.
Grammar
The syntax of the switch statement in the Go programming language is as follows:
switch var1 {
case val1:
...
case val2:
...
default:
...
}
Variable var1
can be any type, val1
and val2
can be any value of the same type. Types are not limited to constants or integers, but must be of the same type; or the end result is an expression of the sametype.
You can test multiple values that may meet the criteria simultaneously, using commas to separate them, such as case val1
, val2
, val3
.
Flow chart:
Example
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("good\\n" )
case grade == "D" :
fmt.Printf("pass\\n" )
case grade == "F":
fmt.Printf("fail\\n" )
default:
fmt.Printf("poor\\n" );
}
fmt.Printf("Your level is %s\\n", grade );
}
The result of the above code execution is:
Excellent!
Your level is A
Type Switch
The switch
statement can also be used to determine the type of variable actually stored in a interface
variable.
Type Switch
syntax format is as follows:
switch x.(type){
case type:
statement(s);
case type:
statement(s);
/* You can define any number of cases */
default: /* optional */
statement(s);
}
Example
package main
import "fmt"
func main() {
var x interface{}
switch i := x.(type) {
case nil:
fmt.Printf(" Type of x:% T",i)
case int:
fmt.Printf("X is an int type")
case float64:
fmt.Printf("X is a float64 type")
case func(int) float64:
fmt.Printf("X is func (int) type")
case bool, string:
fmt.Printf("X is bool or string type" )
default:
fmt.Printf("Unknown type")
}
}
The result of the above code execution is:
Type of x:<nil>
Fallthrough
Using fallthrough
will force the subsequent case
statements to be executed, fallthrough
will not determine whether the expression result of the next case
is true
.
Example
package main
import "fmt"
func main() {
switch {
case false:
fmt.Println("1、case conditional statement is false")
fallthrough
case true:
fmt.Println("2、case conditional statement is true")
fallthrough
case false:
fmt.Println("3、case conditional statement is false")
fallthrough
case true:
fmt.Println("4、case conditional statement is true")
case false:
fmt.Println("5、ccase conditional statement is false")
fallthrough
default:
fmt.Println("6、default case")
}
}
The result of the above code execution is:
2. The case conditional statement is true
3. Case conditional statement is false
4. The case conditional statement is true
As you can see from the output of the above code: switch
judging from the first, the expression is true
of case
start execution, if case
with fallthrough
, the program will proceed to the next case
and it doesn’t judge the next one, and it will not determine whether the expression of the next case
is true
.