由于类型不匹配错误(int vs bool),以下程序无法编译
package main
import "fmt"
func main() {
i := 5
switch i {
case 4:
fmt.Println("4")
case i > 8:
fmt.Println("i is greator than 8")
}
}
作为具有动态打字背景的人,以上内容有点文化冲击。所以想知道在 GO 中执行此操作的惯用方法是什么?
最佳答案
只需使用通用开关:
func main() {
i := 5
switch {
case i == 4:
fmt.Println("4")
case i > 8:
fmt.Println("i is greator than 8")
default:
fmt.Printf("i = (%v), i != 4 && i <= 8\n", i)
}
}
关于go - 开关盒不同类型与去,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32160648/