I'm simply trying to use a type switch for handling times, float32s and float64s. It's working fine for times and float64s, but strconv.FormatFloat(val, 'f', -1, 32)
keeps telling me that I can't use type float32
as type float64
. I don't see how that could be happening, so I must be missing something or misunderstanding how I'm supposed to be calling FormatFloat()
for float32s
.
func main() {
fmt.Println(test(rand.Float32()))
fmt.Println(test(rand.Float64()))
}
func test(val interface{}) string {
switch val := val.(type) {
case time.Time:
return fmt.Sprintf("%s", val)
case float64:
return strconv.FormatFloat(val, 'f', -1, 64)
case float32:
return strconv.FormatFloat(val, 'f', -1, 32) //here's the error
default:
return "Type not supported!"
}
}
Error:
cannot use val (type float32) as type float64 in argument to strconv.FormatFloat