LoginSignup
4
6

More than 5 years have passed since last update.

Go言語 識別子のエクスポート

Last updated at Posted at 2015-07-11

■ 同じパッケージ

同じ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: はろう
4
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
6