value3 :=[...]int8{0,1,2,3,4,5,6}switch value3[4]{case0,1,2:
fmt.Println("0 or 1 or 2")case2,3,4:
fmt.Println("2 or 3 or 4")case4,5,6:
fmt.Println("4 or 5 or 6")}
但是可以用索引方式绕过限制,比如下面这种就可以通过编译:
value5 :=[...]int8{0,1,2,3,4,5,6}switch value5[4]{case value5[0], value5[1], value5[2]:
fmt.Println("0 or 1 or 2")case value5[2], value5[3], value5[4]:
fmt.Println("2 or 3 or 4")case value5[4], value5[5], value5[6]:
fmt.Println("4 or 5 or 6")}
对于类型判断的switch,无法使用索引绕过,编译器会识别出byte是uint8重复,比如:
value6 :=interface{}(byte(127))switch t := value6.(type){caseuint8,uint16:
fmt.Println("uint8 or uint16")casebyte:
fmt.Printf("byte")default:
fmt.Printf("unsupported type: %T", t)}