子包中的可见性

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

Visibility in sub-packages

问题

成员从子包到根包的可见性如何?

我的意思是:

foo          // 根包
foo/utils    // 子包
foo/tools    // 另一个子包

foo能否访问foo/utilsfoo/tools的私有成员,还是它们作为独立的包?

英文:

How's the visibility of members from a sub-package to its root package?

This is what I mean:

foo          // the "root" package
foo/utils    // a sub-package
foo/tools    // another sub-package

Can foo access private members of foo/utils and foo/tools or do they act as separate, independant packages?

答案1

得分: 16

Go没有子目录或子包的概念。包是彼此独立的。导入路径<code>"foo/utils"</code>只是一个导入路径(一种如何找到包的方法)-字符串<code>"foo/utils"</code>除了在本地磁盘或互联网上定位包之外没有其他意义。

<code>foo</code>无法访问<code>foo/utils</code>的私有成员。

在Go1中,源代码树中的每个目录对应一个单独的包。关于此更多信息可以在这里找到:go命令

英文:

Go has no concept of sub-directories nor sub-packages. Packages are separate from each other. The import path <code>"foo/utils"</code> is just an import path (a method of how to find the package) - the string <code>"foo/utils"</code> has no significance other than locating the package on local disk or in the Internet.

<code>foo</code> cannot access private members of <code>foo/utils</code>.

In Go1, each directory in a source tree corresponds to a single package. More about this can be found here: the go command.

huangapple
  • 本文由 发表于 2012年2月16日 01:58:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/9298676.html
匿名

发表评论

匿名网友

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

确定