Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cannot refer to unexported name m.β

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?

like image 837
Dog Avatar asked Aug 27 '14 02:08

Dog


3 Answers

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

http://play.golang.org/p/6KxF5-Cq8P

like image 66
fabrizioM Avatar answered Oct 27 '22 06:10

fabrizioM


β is lowercase, so it is not exported and can't be used from outside that package.

fmt.Println(unicode.IsLower('β'))

playground

like image 43
OneOfOne Avatar answered Oct 27 '22 06:10

OneOfOne


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

like image 26
Shenal Silva Avatar answered Oct 27 '22 05:10

Shenal Silva