比如, case int, float64 编译会报错,有没有什么方法能实现这种用法呢?
```
package main
import "fmt"
func do(i interface{}) {
switch v := i.(type) {
case int, float64: // Illegal! Then how?
fmt.Printf("Twice %v is %v\n", v, v*2)
case string:
fmt.Printf("%q is %v bytes long\n", v, len(v))
default:
fmt.Printf("I don't know about type %T!\n", v)
}
}
func main() {
do(21)
do(3.13)
do("hello")
do(true)
}
```
有疑问加站长微信联系(非本文作者)