无法引用未导出的名称 m.β

huangapple go评论68阅读模式
英文:

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 (&quot;m&quot;;&quot;fmt&quot;)
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(&#39;β&#39;))
}

returns

false

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

答案2

得分: 3

β是小写字母,因此它不会被导出,不能从包外部使用。

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

playground

英文:

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

fmt.Println(unicode.IsLower(&#39;β&#39;))

<kbd>playground</kbd>

答案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 (
	&quot;fmt&quot;
	&quot;unicode&quot;
)

const Β = 1

func main() {
	const (
		GreekLowerβ = &#39;β&#39;
		GreekUpperΒ = &#39;Β&#39;
	)
	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

huangapple
  • 本文由 发表于 2014年8月27日 10:01:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/25517826.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定