This is the eighth chapter of Golang Language Learning Tutorial
switchis a conditional statement that compares the value of an expression to a list of options that might match, and executes the code as appropriate. is a if else common way to replace.
The following is a simple operational procedure to understand the switch statement
package mainimport "fmt"func eval(a, b int, op string) int { //定义eval函数,输入三个值 a, b, op 输出类型为 int var result int switch op { // op 为表达式,将 op 的值与下面 case 比较 case "+": //若 op 值为 + , 则将 result 赋值 a + b 以此类推 result = a + b case "-": result = a - b case "*": result = a * b case "/": result = a / b default: //如果输入没有以上匹配的字符,则属于默认情况,运行默认情况 fmt.Println("unsupported operator:" + op) //输出不识别 op 值的报错 } return result}func main(){ fmt.Println( eval(10, 4, "+"), eval(15, 6, "-"), eval(2, 4, "*"), eval(100, 4, "/"), )}
In the above procedure, the switch value of OP is used as an expression, case compared with. If the OP value is "+" , it matches to the first one case , and then exits when the result is assigned a value case . And so on
If the OP input value does not match, it is the default, the 运行默认代码 program output error.
The results of the above program execution are:14 9 8 25
Multi-expression judgment
, case Multiple expressions can be written in one by separating them with commas
func main(){ letter := "i" switch letter { case "a", "o", "e", "i", "u": //用 "," 逗号分隔,多个表达式 fmt.Println(letter, "is in vowel") default: fmt.Println(letter, "is not in vowel") }}
In case a ,,, e i o , u : In this line, all the vowels are listed. As long as the item is matched, the outputi is in vowel
Switch without an expression
switch, the expression is optional, and if you omit the expression, the switch statement is equivalent to each of which is considered switch true case valid and executes the corresponding block of code.
func main(){ num := 79 switch { //省略表达式 case num >= 0 && num <= 50: fmt.Println("num is greater than 0 and less than 50") case num >= 50 && num <= 100: fmt.Println("num is greater than 50 and less than 100") case num >= 100: fmt.Println("num is greater than 100") }}
In the above program, the NUM value is 79 and the expression is omitted, matching each case until the end.
Output output of the above program:num is greater than 50 and less than 100
Fallthrough statements
In Go, after each case is executed, it jumps out of the switch statement and does not judge and execute the subsequent case. Use a fallthrough statement to transfer control to the next execution code after the completed case has been executed case .
Or the previous program, but if it will be num assigned a value of 100, will only match to the second case after jumping out, we added fallthrough after viewing the effect.
func main(){ num := 79 switch { //省略表达式 case num >= 0 && num <= 50: fmt.Println("num is greater than 0 and less than 50") case num >= 50 && num <= 100: fmt.Println("num is greater than 50 and less than 100") fallthrough case num >= 100: fmt.Println("num is greater than 100") }}
num := 100, you can configure the second case, or the third one, by fallthrough which we successfully matched to two case.
NOTE: fallthrough The statement should be the last statement of the case clause. Do not write in the middle of a case statement.
The above for Learning Golang switch article