Constant expressions like 'A' or 3 or named constants like "const x = 7"
are what Go calls "untyped constants". The type of these constants are
determined by the context in which they're used. For example:

const myConst = 3
myFloat := 2.5
fmt.Println(myFloat + myConst)
fmt.Println(myFloat + 3)

Both of the above cases work because myConst and the literal 3 are untyped
constants that take on the type float64 automatically.

You can also declare typed constants, which no longer have these type
inference properties.

const myConst int = 3
myFloat := 2.5
fmt.Println(myFloat + myConst)  // No longer works

If you're curious about the details, I would check out the section of the
language spec on this: https://golang.org/ref/spec#Constants

On Mon, Feb 11, 2019 at 9:37 AM Jamie Caldwell <[email protected]>
wrote:

> Hello,
>
> Can you help?
>
> https://play.golang.org/p/XfJZ3h06p60
>
> Why does 'A' work, when first assigning it to a variable doesn't?
>
> Thank you,
> Jamie.
>
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to