以下是关于Go包的断言准确吗?

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

Are the following assertions about Go packages accurate?

问题

以下是关于Go包的断言是否准确的翻译:

  1. import "package_name" 导入了位于名为 package_name 的目录中的所有文件,假设该目录在 $GOPATH 中找到,$GOPATH 是保存用户 Go 目录的变量,或者在标准 Go 安装目录树中找到。

  2. package_name 目录中的文件通常会声明 package package_name。但是这并非必需。实际上,如果在导入的 package_name 目录中找到了一行包含 package foo 的文件,那么 import "package_name" 也会导入该文件。

  3. 所有首字母大写的函数将通过 package_name 声明中给定的名称进行访问,例如:

package_name.声明package_name的文件中的函数 或者 other_than_package_name.声明other_than_package_name的文件中的函数

  1. 用户定义的包可以通过命令行的 go install 在包目录中安装。然而,Go 将拒绝安装与其内置包目录同名的目录。例如,你不能安装一个名为 strings 的目录,因为 Go 已经有一个用于内置包 "strings" 的 strings 目录。然而,用户可以通过创建一个名为 my_strings 的目录,并在其中放置一个声明 package strings 的文件,将函数附加到 strings 包中而不改变内置的 strings 文件夹。现在,import my_strings 将加载额外的用户定义的 strings 函数,并通过 strings.Function_name 进行访问。

总之,import 关键字用于从给定目录加载文件。而关键字 package 创建了一个命名空间,用于从文件外部访问首字母大写的函数。

我对上述内容的理解是否正确?

英文:

Are the following assertions about Go packages accurate?

  1. import "package_name" imports all files from the directory called package_name assuming is found in $GOPATH, a variable that holds the users go directory, or in the standard go installation directory tree.

  2. files within the package_name directory will usually state package package_name. But they are not required to. In fact, import "package_name", would also import a file including the line package foo if the file was found in the imported package_name directory.

  3. All functions that are Capitalized will be accessed through the name given in the package package_name declaration--for instance:

package_name.Function_in_file_that_declares_package_name or other_than_package_name.Function_in_file_that_declares_other_than_package_name

  1. User defined packages are command-line go install-ed from the within the package directory. However, go will refuse to install a directory named identically to its builtin package directories. For instance you cannot install a strings directory since go already has a strings directory for the builtin package "strings." However, the user can append functions to the strings package without altering the builtin strings folder, by creating a my_strings directory and the placing a file that states package strings within it. Now, import my_strings will load the extra user-defined strings functions accessed with strings.Function_name.

In summary, the import keyword is used to load files from a given directory. And the keyword package creates a namespace to access Capitalized functions from outside that file.

Am I understanding all of the above correctly?

答案1

得分: 4

  1. "import"的参数是一个import_path,而不是一个包名。它使得位于$GOPATH/src/import_path下的包中导出的实体在出现"import"语句的文件范围内可用。

  2. 在同一个目录下的所有*.go文件(除了*_test.go文件和带有// +build ignore的文件)必须在package name语句中声明相同的名称,否则Go构建系统将拒绝它。

  3. 不是大写字母开头,但属于Unicode类别Lu。不是函数,而是任何顶级域名实体。

  4. 不,你可以使用其导入路径从任何目录中go-install任何包。是的,stdlib中的包具有优先级,不能被"覆盖"。然而,你可以使用例如import strings "github.com/foo/mystrings"来有效地"替换"一个stdlib包。然而,这种效果仅限于本地/文件。

总之,不,import用于使其他包中的实体在文件范围内可用。关键字"package"不创建命名空间。"import"的效果是文件范围的,参见前面的句子,通常导入的实体通过限定名称来引用。该限定符是一种命名空间,但请注意,不是"导出者"(package foo)控制它。相反,控制权在"导入者"一侧:import whatever_local_name "whatever_import_path"。默认的限定符是导入路径的基本名称。

"我们都同意了吗?"

完全不同意。

英文:
  1. The argument of "import" is an import_path, not a package name. It makes exported entities from a package found in $GOPATH/src/import_path available in the file scope where the "import" clause appears.

  2. All *.go files, except *_test.go files and files with // +build ignore, in a single directory must say the same name in the package name clause or the go build system will reject it.

  3. Not capitalized, but belonging to the Unicode category Lu. Not functions but any TLD entity.

  4. No, you can go-install any package from any directory using its import path. Yes, packages from the stdlib have priority and cannot be "overriden". However, you can effectively "replace" a stdlib package using eg. import strings "github.com/foo/mystrings". However, the effect is local/file only.

In summary, no, import is used to make entities from other packages available in the file scope. The keyword "package" creates no namespace. The effect of "import" is file-scoped, see the preceding sentence and usually the imported entities are referred to by qualified names. That qualifier is a sort of namespace, but note that not the "exporter" (package foo) controls that. Instead the control is at the "importer" side: import whatever_local_name "whatever_import_path". The default qualifier is the basename of the import path, though.

"Do we all agree?"

Not at all.

答案2

得分: 4

即使听起来很严厉:几乎所有的假设都是完全错误的。

请查看http://golang.org/doc/effective_go.html#package-names和http://golang.org/ref/spec#Packages,不要将import视为Go中include的等价物。

Go的包更像是预编译的库,import "some/path/foo"更像是链接foo.a(但也使得导出的实体在通常情况下可用于foo.SomethingExported)。

现在请查看http://golang.org/doc/code.html,应该清楚了什么是包以及如何使用它们。

英文:

Even if it sounds harsh: Almost all assumptions are completely wrong.

Take a look at http://golang.org/doc/effective_go.html#package-names and http://golang.org/ref/spec#Packages and do not think of import as the Go equivalent of C's include.

Go's packages are more like precompiled libraries and an import "some/path/foo" is more like linking in foo.a (but also makes exported entities available under - normaly - foo.SomethingExported.

Now have a look at http://golang.org/doc/code.html and it should become clear what packages are and how they are used.

huangapple
  • 本文由 发表于 2013年8月20日 16:19:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/18330184.html
匿名

发表评论

匿名网友

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

确定