go中interface类型很好用,但是有时候用了就没办法作为键值了 这点就比较头痛了,这里介绍一种转换方法,非常好用
func GetInterfaceToInt(t1 interface{}) int {
var t2 int
switch t1.(type) {
case uint:
t2 = int(t1.(uint))
break
case int8:
t2 = int(t1.(int8))
break
case uint8:
t2 = int(t1.(uint8))
break
case int16:
t2 = int(t1.(int16))
break
case uint16:
t2 = int(t1.(uint16))
break
case int32:
t2 = int(t1.(int32))
break
case uint32:
t2 = int(t1.(uint32))
break
case int64:
t2 = int(t1.(int64))
break
case uint64:
t2 = int(t1.(uint64))
break
case float32:
t2 = int(t1.(float32))
break
case float64:
t2 = int(t1.(float64))
break
case string:
t2, _ = strconv.Atoi(t1.(string))
break
default:
t2 = t1.(int)
break
}
return t2
}