I have a variable, data
, which is an interface. When I print its type, I get it as json.Number. How do I typecast to int/int64/float64?
If I try data.(float64), it ends up with a panic error:
panic: interface conversion: interface {} is json.Number, not float64
data.(json.Number).Int64()
You type assert to the actual underlying type, not to an arbitrary type you wish it was. That means that if an interface's underlying type isjson.Number
you type assert it asjson.Number
. And alsov.(T)
in Go is not conversion but type assertion. – Opponentjson.Number
's underlying type may bestring
the two types are not the same and so type-asserting one to the other is bound to fail, however converting one to the other is ok. – Opponent