doucaigai7176 2013-08-20 08:19
浏览 9

以下有关Go软件包的主张正确吗?

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?

  • 写回答

2条回答 默认 最新

  • duanmei1850 2013-08-20 08:30
    关注
    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.

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度