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
The right way to do it would be like so: https://play.golang.org/p/_evcLsjcXY-
var x interface{}
x = int64(12)
var y float64
switch v := x.(type) {
case int:
y = float64(v)
case int8:
y = float64(v)
case int16:
y = float64(v)
case int32:
y = float64(v)
case int64:
y = float64(v)
case uint:
y = float64(v)
case uint8:
y = float64(v)
case uint16:
y = float64(v)
case uint32:
y = float64(v)
case uint64:
y = float64(v)
case float32:
y = float64(v)
case float64:
y = v
}
fmt.Println(y)
If you don't care about performance / safety / etc., and just want something short (don't do this):
var x interface{}
x = int64(12)
tmp, _ := json.Marshal(x)
var y float64
json.Unmarshal(tmp, &y)
fmt.Println(y)
Or, you could use reflection https://play.golang.org/p/QYQP00KicmF:
import "reflect"
var floatType = reflect.TypeOf(float64(0))
func getFloat(unk interface{}) (float64, error) {
v := reflect.ValueOf(unk)
v = reflect.Indirect(v)
if !v.Type().ConvertibleTo(floatType) {
return 0, fmt.Errorf("cannot convert %v to float64", v.Type())
}
fv := v.Convert(floatType)
return fv.Float(), nil
}