如何在switch中对多个type进行匹配

kooder · 2017-10-12 11:11:54 · 3977 次点击
golwei
没有永恒.

switch是一个筛选器,搞多条件感觉有点违法设计思想.

#3
更多评论

不能偷懒,老老实实分开呗。

#1

如果你这里把int和float64分到同一组是出于有相同的算数操作,那么可以考虑把这些操作归类到一个interface,然后将int和float64再自定义类型,type MyInt inttype MyFloat64 float64, 然后将MyInt和MyFloat64实现这个interface的接口,这样上面就可以统一为 :

case MyInterface:
    fmt.Printf("Twice %v is %v\n", v, v.Multiply(2))
#2