英文:
cannot refer to unexported name m.β
问题
请看一下这两个简单的包:
package m
const β = 1
<!
package main
import ("m";"fmt")
func main() {
fmt.Println(m.β)
}
当我尝试编译它们时,我得到了这个错误:
$ GOPATH=`pwd` go run a.go
# command-line-arguments
./a.go:4: cannot refer to unexported name m.β
./a.go:4: undefined: m.β
为什么?我尝试将两个包中的β替换为B,它可以工作,但我想在这里使用正确的符号。也许这两个包使用了同形异义字或不同的编码方式?
英文:
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?
答案1
得分: 13
根据Go语言规范,如果标识符的名称的第一个字符是Unicode大写字母(Unicode类别"Lu"),则该标识符被导出。
func main() {
fmt.Println(unicode.IsUpper('β'))
}
返回结果为false
。
参考链接:http://play.golang.org/p/6KxF5-Cq8P
英文:
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
答案2
得分: 3
β是小写字母,因此它不会被导出,不能从包外部使用。
fmt.Println(unicode.IsLower('β'))
英文:
β is lowercase, so it is not exported and can't be used from outside that package.
fmt.Println(unicode.IsLower('β'))
答案3
得分: 1
一个在导出包中的函数或方法需要以大写字母开头。昨天遇到了同样的问题,可以参考这个链接:https://stackoverflow.com/questions/25501875/error-in-importing-custom-packages-in-go-lang
英文:
A function , method in an exported package needs to start with an upper case letter. Ran into the same problem yesterday https://stackoverflow.com/questions/25501875/error-in-importing-custom-packages-in-go-lang
答案4
得分: 1
导出标识符的名称的第一个字符必须是一个 Unicode 大写字母。例如,
package main
import (
"fmt"
"unicode"
)
const Β = 1
func main() {
const (
GreekLowerβ = 'β'
GreekUpperΒ = 'Β'
)
fmt.Println(GreekLowerβ, unicode.IsUpper(GreekLowerβ))
fmt.Println(GreekUpperΒ, unicode.IsUpper(GreekUpperΒ))
}
输出结果:
946 false
914 true
> Go 编程语言规范
>
> 导出标识符
>
> 为了允许从另一个包中访问标识符,可以将其导出。如果满足以下两个条件,则标识符被导出:
>
> 1. 标识符名称的第一个字符是一个 Unicode 大写字母(Unicode 类别为 "Lu");
> 2. 标识符在包块中声明,或者是字段名或方法名。
>
> 所有其他标识符都不会被导出。
希腊字母表: Β β beta
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论