Have a look at these two simple packages:
package m
const β = 1
package main
import ("m";"fmt")
func main() {
fmt.Println(m.β)
}
I get this error when I try to compile them:
$ GOPATH=`pwd` go run a.go
# command-line-arguments
./a.go:4: cannot refer to unexported name m.β
./a.go:4: undefined: m.β
Why? I tried replacing the β with B in both packages, and it works, but I'm trying to use the proper symbol here. Maybe both packages are using homoglyphs or different encodings for some reason?
The go specifications say that an identifier is exported if
the first character of the identifier's name is a Unicode upper case letter (Unicode class "Lu")
https://golang.org/ref/spec#Exported_identifiers
func main() {
fmt.Println(unicode.IsUpper('β'))
}
returns
false
β is lowercase, so it is not exported and can't be used from outside that package.
fmt.Println(unicode.IsLower('β'))
A function , method in an exported package needs to start with an upper case letter. Ran into the same problem yesterday Error in importing custom packages in Go Lang
The first character of an exported identifier's name must be a Unicode upper case letter. For example,
package main
import (
"fmt"
"unicode"
)
const Β = 1
func main() {
const (
GreekLowerβ = 'β'
GreekUpperΒ = 'Β'
)
fmt.Println(GreekLowerβ, unicode.IsUpper(GreekLowerβ))
fmt.Println(GreekUpperΒ, unicode.IsUpper(GreekUpperΒ))
}
Output:
946 false
914 true
The Go Programming Language Specification
An identifier may be exported to permit access to it from another package. An identifier is exported if both:
- the first character of the identifier's name is a Unicode upper case letter (Unicode class "Lu"); and
- the identifier is declared in the package block or it is a field name or method name.
All other identifiers are not exported.
Greek alphabet: Β β beta