Dog
Dog

Reputation: 7787

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?

Upvotes: 10

Views: 6871

Answers (4)

peterSO
peterSO

Reputation: 166598

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

Exported identifiers

An identifier may be exported to permit access to it from another package. An identifier is exported if both:

  1. the first character of the identifier's name is a Unicode upper case letter (Unicode class "Lu"); and
  2. 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

Upvotes: 1

fabmilo
fabmilo

Reputation: 48330

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

Upvotes: 13

Shenal Silva
Shenal Silva

Reputation: 2023

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

Upvotes: 1

OneOfOne
OneOfOne

Reputation: 99244

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

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

playground

Upvotes: 3

Related Questions