package main
import "fmt"
func main() {
printType(4.0)
}
func printType(i interface{}) {
switch i := i.(type) {
case float32:
fmt.Println("This is a float type", i)
}
}
For some reason this code does not detect 4.0
value as float32
but it detects it as float64
- why is that? I run it on win x64 machine.
For some reason this code does not detect 4.0 value as float32 but it detects it as float64 - why is that?
Because float64
is the default type for an untyped floating-point constant. Relevant part emphasized:
An untyped constant has a default type which is the type to which the constant is implicitly converted in contexts where a typed value is required, for instance, in a short variable declaration such as i := 0 where there is no explicit type. The default type of an untyped constant is bool, rune, int, float64, complex128 or string respectively, depending on whether it is a boolean, rune, integer, floating-point, complex, or string constant.