Under what circumstances could this code:
v, ok := value.(int64)
if !ok {
panic("NOPE "+reflect.TypeOf(value).Kind().String())
} else {
fmt.Printf("VAL: %d
",v)
}
produce the panic with message panic: NOPE int64
?
Is this a bug or is there something basic about the numeric types that I'm missing?
This can happen if you're using type declaration on numeric types. If you do something like this:
type T int64
...
var value interface{} = T(1)
and put it into your code, you'll get the exact same error. But if you don't check the kind, but the type, you'll see what's happening here:
v, ok := value.(int64)
if !ok {
panic("NOPE " + reflect.TypeOf(value).String())
} else {
fmt.Printf("VAL: %d
", v)
}
produces the message:
panic: NOPE main.T
The kind
of T is int64, but value
is not int64.