英文:
Setting package path in Go
问题
开始学习Go并阅读这里的文档:https://golang.org/doc/code.html
标题为“Package paths”的部分建议使用我的Github帐户作为基本路径。然而,在GH网址中使用正斜杠时,当我运行mkdir -p $GOPATH/src/github.com/user
时,它会创建一个子文件夹。所以github.com/user
的示例会创建:
src/
github.com/
user/
但我认为这不是预期的结果。
我应该怎么做?
英文:
Getting started with Go and going through the docs here: https://golang.org/doc/code.html
The part entitled Package paths recommends using my Github account as a base path. However, with the forward slashes in the GH url, when I run mkdir -p $GOPATH/src/github.com/user
it creates a sub-folder. So the example of github.com/user
creates:
src/
github.com/
user/
but I don't think this is what's intended.
What can I do here?
答案1
得分: 6
行为是正确的。Go语言中的包名提供了唯一的全局命名空间。
因此,github.com/user/repo
标识了一个包,可以很容易地通过 go get
进行下载和安装,并提供了非常需要的分离。你也可以创建没有主机名(在这种情况下是 github.com)的包,但这样会有效地阻止用户使用 go get
,而需要手动管理。
在GitHub的情况下,使用用户名允许你使用其他库的分支并保持分离。然后,完整的包名用于导入:
import "github.com/user/repo"
英文:
The behavior is correct. The packages names in Go provide unique global name space.
github.com/user/repo
therefore identifies a package, which is easily go get
-able (download and install the package) and also provides much needed separation. You can also create packages without a hostname (in this case github.com) but effectively preventing users from using go get
and resorting to manual management.
Having a username in GitHub case allows you to use forks of other libraries and maintain the separation. The full package name is then used for importing
import "github.com/user/repo"
答案2
得分: 4
这实际上是预期的行为,
你甚至可以在 GitHub 存储库上运行 go get
命令,它会创建相同的目录结构。
英文:
This is actually the intended behavior,
You can even call go get
on a github repo and it will create this same directory structure
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论