Is the default type of a constant changed

Can I say the type of the integer constant “12” is converted from the default type “int” to “int32” when it is assigned to the constant “n1” whose type is “int32” ?

const n1 int32 = 12

Go programmimg language specification says “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.”

No, not really. “12” is an untyped integer constant. If you use it in a context where the type isn’t specified but a type is needed, it defaults to int. So these two are equivalent:

a := 12
b := int(12)

In your example, there is a context in which to interpret 12: const n1 int32. This means that for the assignment to work, 12 needs to be an int32. But it was never int, so there’s no real conversion happening.

2 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.