I'm using a library that accepts only float64
as a function argument but my code can post any type of numbers in an interface{}
, meaning int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64
.
How can I write an elegant function that assert for all these types ?
I was thinking of something like:
if i, ok := event.Value.(int); ok {
value = float64(i)
} else if f, ok := event.Value.(float64); ok {
value = f
} else {
... error and return
}
with one else if
per type.
I'm sure there's something more efficient/elegant somewhere!
Thanks