switch v := v.(type) 求解

DonWang · · 2267 次点击
如果你学会正常写代码,你就不会写 `switch v:= v.(type)` 这样的代码,哪怕你换一个 `v2 := v.(type)`,你就能发现自己的代码有什么问题
#4
更多评论
#### 输出结果 ```shell *int true *int or *string false *int or *string false ```
#1
case 后面跟多个类型时,只要其中一个类型匹配, 会直接将v赋值给k,类似执行k:=v ```go switch k := v.(type) { case *int, *string: fmt.Println("*int or *string", k == nil) case int: fmt.Println("int", k) } ```
#2