I often have a situation, where I expect an int
(of any type, int/int8/16/32/64
) and check for it using a type switch
switch t := v.(type) {
case int, int8, int16, int32, int64:
// cast to int64
case uint, uint8, uint16, uint32, uint64:
// cast to uint64
}
Now I cannot use a direct cast, because t
in this case will be of type interface{}
. Do I really have to split this up into case
s for each integer type?
I know that I could do it via reflection using reflect.ValueOf(v).Int()
, but shouldn't there be a better (less verbose) way of doing this?
UPDATE:
Filed an issue, and Rob advised to just use reflect
in this case.