Constants
…
Constants may be typed or untyped.
Now that’s interesting.
And powerful.
This is one of the features of Go I featured in my recent video 10 Reasons I Like Go.
This feature really helps bridge the gap between the convenience of dynamically typed languages, and the safety of statically typed languages.
Let’s look at the details…
Literal constants,
true
,false
,iota
, and certain constant expressions containing only untyped constant operands are untyped.
It might be surprising that true
and false
are untyped. Shouldn’t they be of type bool
? Well, consider that in Go we can define custom types based on bool
, and we may want to assign these literals to such a value/constant. For example:
// SpanishBool works just like bool, but renders as "verdad"/"falso" rather than "true"/"false"
type SpanishBool bool
func (b SpanishBool) MarshalText() ([]byte, error) {
if b {
return []byte("verdad"), nil
}
return []byte("falso"), nil
}
const isValid = true // isValid is untyped, not of type bool
/* then later ... */
var accountIsValid SpanishBool = isValid // assign untyped isValid constant to
// variable of type SpanishBool
A constant may be given a type explicitly by a constant declaration or conversion, or implicitly when used in a variable declaration or an assignment statement or as an operand in an expression. It is an error if the constant value cannot be represented as a value of the respective type. If the type is a type parameter, the constant is converted into a non-constant value of the type parameter.
But of course, you can have typed constants, too. This happens when you explicitly give a constant a type, or when the type is inferred from the constant expression.
const isValid = bool(true)
In this case, it is impossible to assign the constant to a variable/constant of another type, without explicit conversion:
const isValid = bool(true)
var x SpanishBool = isValid
will result in:
cannot use isValid (constant true of type bool) as type SpanishBool in variable declaration
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 isbool
,rune
,int
,float64
,complex128
orstring
respectively, depending on whether it is a boolean, rune, integer, floating-point, complex, or string constant.
And finally, untyped constants have a default type, which is used whenever an exact type isn’t otherwise specified. To demonstrate:
const x = 123
const y = 123.0
fmt.Printf("x's type: %T\ny's type: %T\n", x, y)
will print:
x's type: int
y's type: float64
Quotes from The Go Programming Language Specification, Version of June 29, 2022