typed/untyped constant

Topic

golangのconstantについて

The Go Blog

Summary

constantにはtypedとuntypedの二種類がある

Default type

typed

const typedHello string = "Hello, world"

untyped

"Hello, world"
const hello = "Hello, world"

この場合、 str のtypeは?

//右オペランドはuntyped constant
str := "Hello, world"

untyped constantは default type がある

↑の場合は、"Hello, world" のdefault typeはstring → strのtypeはstring

The answer is that an untyped constant has a default type, an implicit type that it transfers to a value if a type is needed where none is provided.

なので、これも同じ

var str = "Hello, world"

最後に

変数はuntypedない

How's day been?

Good 😉