Go包命名约定背后的理念是什么?

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

What is the idea behind Go package naming convention?

问题

Go语言中的包命名约定是为了方便包的导入和使用。大多数包的导入路径都是以类似以下的形式安装和导入的:

import "github.com/howeyc/fsnotify"

我理解包名应该是唯一的,但是我不明白为什么要使用github.com这样的网址。为什么不直接使用作者/包名的形式呢?像这样:

import "howeyc/fsnotify"

这样的命名方式不太可能发生冲突。或者还有其他更短的命名策略吗?是因为它与go get命令“只是工作”吗?还是有其他原因?

英文:

I'm trying to understand the idea behind package naming convention in Go. Most packages are installed and imported as something like:

import "github.com/howeyc/fsnotify"

I get the idea that package names should be unique, but I don't see the point of using the website github.com. Why not just use author/package? Like:

import "howeyc/fsnotify"

That's not likely to ever collide. Or some other "shorter" strategy? Is it because it "just works" with go get? Or is there some other reason?

答案1

得分: 9

你可以使用howeyc/fsnotify。当使用github.com/howeyc/fsnotify时,可以理解为该包托管在Github上。其他仓库也可以使用。

原因是使用go get更容易找到和安装依赖项。否则,你将不得不手动满足依赖项。由于在开源世界中经常会fork仓库,你可能会有同一作者的修改版本。因此,有助于区分你的项目依赖于什么。

英文:

You can use howeyc/fsnotify if you want to. When github.com/howeyc/fsnotify is used it's understood that the package is hosted on Github. Other repositories work as well.

The reason is it makes it easier to locate and install dependencies with go get. Otherwise you'd have to satisfy the dependencies manually. And since forking repos is quite common in the open-source world, you may have a modified version from the same author. So it helps to distinguish what your project depends on.

答案2

得分: 1

> 下载并安装包和依赖项
>
> 用法:
>
> go get [-d] [-fix] [-u] [构建标志] [包]
>
> Get 命令会下载并安装由导入路径指定的包,以及它们的依赖项。
>
> 当检出或更新一个包时,get 命令会查找与本地安装的 Go 版本匹配的分支或标签。最重要的规则是,如果本地安装的版本是 "go1",get 命令会查找名为 "go1" 的分支或标签。如果不存在这样的版本,它会获取包的最新版本。
>
> 有关指定包的更多信息,请参阅 'go help packages'。
>
> 有关 'go get' 如何查找要下载的源代码的更多信息,请参阅 'go help remote'。

导入路径支持 go get 命令。表示远程仓库的路径以代码路径开头。有关详细信息,请运行 go help remote 命令。

英文:

> Download and install packages and dependencies
>
> Usage:
>
> go get [-d] [-fix] [-u] [build flags] [packages]
>
> Get downloads and installs the packages named by the import paths,
> along with their dependencies.
>
> When checking out or updating a package, get looks for a branch or
> tag that matches the locally installed version of Go. The most
> important rule is that if the local installation is running version
> "go1", get searches for a branch or tag named "go1". If no such
> version exists it retrieves the most recent version of the package.
>
> For more about specifying packages, see 'go help packages'.
>
> For more about how 'go get' finds source code to download, see 'go help remote'.

The import path supports the go get command. Paths denoting remote repositories begin with the path to the code. Run the go help remote command for details.

huangapple
  • 本文由 发表于 2013年8月6日 05:30:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/18068096.html
匿名

发表评论

匿名网友

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

确定