When declaring two const variables (one typed and one untyped), and printing out the type of the second one, like:
const x float32 = 10000
const y = 1e8 / x
fmt.Printf("the type of y: %T
", y)
// or with reflect:
fmt.Println(reflect.TypeOf(y))
it tells me, that y
is of type float32
.
Well this doesn't come as a surprise, since the the untyped const variable y
is defined as dividing an untyped float constant by a typed float constant and therefore the type might be inferred.
According to "Go by Example" (https://gobyexample.com/constants), constants never have a type, unless explicitly provided. However, according to the official Go blog documentation, untyped constants do have hidden types that get inferred, but let the constant remain untyped until a type is needed.
I would have assumed, that y
would still be an untyped float constant. However, when using it for assigning a new variable, Goland's inspector tells me, that the type can be omitted:
const z float32 = y
// ^^^ type can be ommitted
So finally, my question would be:
Is it impossible to declare an untyped constant out of typed constant expressions?