#
In Go, a name is exported if it begins with a capital letter. For example, Pizza is an exported name, as is Pi, which is exported from the math package.
pizza and pi do not start with a capital letter, so they are not exported.
When importing a package, you can refer only to its exported names. Any "unexported" names are not accessible from outside the package.
Run the code. Notice the error message.
To fix the error, rename math.pi to math.Pi and try it again.
代码
package main
import (
"fmt"
"math"
)
func main() {
fmt.Println(math.pi)
}
翻译
在Go的世界里面,一个东西(函数、变量等等)是否被出口(也就是外面的package可以访问)是看这个东西(函数、变量等等)的第一个
字母是否大写,如果大写,就可以被出口(也就是能为外界的package访问),反之则不行.
pizza和pi这俩都不是以大写字母开头,所以他们没有被出口.
当你import一个package的时候,你只能引用或者说是使用它出口的东西,没有出口的东西,你访问不到,除非是在本包(也就是说,本包是可以访问任何本包的东西的)
总结
一般Java会使用public protected private default-package这种权限级别来组织整个代码的访问权限,但是Go没有试用这种关键字
形式的规范,而是就是通过首字母是否大小写来决定这个东西或者说是资源能不能被外界看到(函数,struct,变量等等都可以称为资源或者东西)
文章永久链接:https://tech.souyunku.com/44589