■ 同じパッケージ
同じpkg内の const
は、先頭が大文字小文字に限らず、使えることが出来ます。
ソースファイルのエンコードが UTF-8
であれば、 日本語
も使えます。
package main
import "fmt"
const (
HELLO = "大文字alphabet"
hello = "小文字alphabet"
はろう = "ひらがな"
)
func main() {
fmt.Println(HELLO)
fmt.Println(hello)
fmt.Println(はろう)
}
結果
大文字alphabet
小文字alphabet
ひらがな
■ 別パッケージからエクスポート
まずエクスポートしてくるpkgは、これ。
pkg-identifer
package identifier
const (
HELLO = "大文字alphabet"
hello = "小文字alphabet"
はろう = "ひらがな"
)
1. 大文字
これは、OK。
package main
import (
"fmt"
. "identifier"
)
func main() {
fmt.Println(Hello)
}
結果
大文字alphabet
2. 小文字
小文字の const
は、エクスポート出来ない。
package main
import (
"fmt"
. "identifier"
)
func main() {
fmt.Println(hello)
}
結果
# command-line-arguments
./main.go:5: imported and not used: "identifier"
./main.go:9: undefined: hello
3. 日本語
日本語には、大文字・小文字の区別がないので、日本語の識別子はエクスポート出来ない。
package main
import (
"fmt"
. "identifier"
)
func main() {
fmt.Println(はろう)
}
結果
# command-line-arguments
./main.go:5: imported and not used: "identifier"
./main.go:9: undefined: はろう