我经常遇到这样的情况,我需要一个int
(任何类型的int/int8/16/32/64
),并使用类型切换进行检查。
switch t := v.(type) {
case int, int8, int16, int32, int64:
// cast to int64
case uint, uint8, uint16, uint32, uint64:
// cast to uint64
}
现在我不能使用直接转换,因为在这种情况下,
t
将是interface{}
类型。我真的需要将其拆分为每个整数类型的case
吗?我知道可以通过使用
reflect.ValueOf(v).Int()
进行反射来完成,但是否应该有更好(更简洁)的方法来完成此操作?更新:提交了一个问题,并建议在这种情况下只使用
reflect
。
int64
,将类型转换留给调用者处理。 - Ilia Cholyfunc(format string, args...interface{})
,这就是这个例子的情况。 - user187676