我知道你可以使用逗号将多个值分隔开,在switch语句中匹配多个值:
func main() {
value := 5
switch value{
case 1,2,3:
fmt.Println("matches 1,2 or 3")
case 4,5, 6:
fmt.Println("matches 4,5 or 6")
}
}
http://play.golang.org/p/D_2Zp8bW5M
我的问题是,能否使用多个值的切片作为case(s)在switch语句中匹配多个值?我知道可以使用if else语句和一个'Contains(slice, element)'函数来实现,只是想知道是否可能。
像这样的东西也许可以吗?
func main() {
value := 5
low := []int{1, 2, 3}
high := []int{4, 5, 6}
switch value {
case low:
fmt.Println("matches 1,2 or 3")
case high:
fmt.Println("matches 4,5 or 6")
}
}
if
,当switch为空时,或者是一个if value ==
,当它不为空时。 - Alex Netkachov