go的switch case
可以一個case帶幾個引數:
var i = 0 switch i { case 0, 1: fmt.Println(“1”) case 2: fmt.Println(“2”) default: fmt.Println(“def”) }
預設有break效果,要取消就加上fallthrough:
var i = 0 switch i { case 0: fallthrough case 1: fmt.Println(“1”) case 2: fmt.Println(“2”) default: fmt.Println(“def”) }
case還可以是表示式:
var i = 0 switch { case i > 0 && i < 10: fmt.Println(“i > 0 and i < 10”) case i > 10 && i < 20: fmt.Println(“i > 10 and i < 20”) default: fmt.Println(“def”) }
注意:go的switch case預設有break。如果不需要break,可以加上fallthrough**~~