Is it better to have golang statements go out to github or have relative path and why?

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

Is it better to have golang statements go out to github or have relative path and why?

问题

在golang的导入语句中,对于项目内部的文件,是更好使用类似以下方式:

import(
    'project/package'
)

还是

import(
    'github.com/owner/project/package'
)

对于这两种选项,为什么你会选择其中一种而不是另一种呢?

我之所以问这个问题,是因为虽然第一种方式更简单和直观,但我也看到很多大型项目(如Kubernetes和Etcd)采用了第二种方式。

英文:

In golang import statements, is it better to have something like:

import(
    'project/package'
)

or

import(
    'github.com/owner/project/package'
)

for files local to your project?

For either option, why would you want to do one over the other?

I ask this because while it's easy and more intuitive to do the first one, I've also seen a lot of big projects like Kubernetes and Etcd do it the second way.

答案1

得分: 4

这些对于go构建工具来说是完全相同的。package在以下两个目录中的位置:

$GOPATH/src/github.com/owner/project/package

或者

$GOPATH/src/project/package

对于go构建工具来说没有任何区别。

唯一的区别是,对于前者,你可以使用go get自动获取源代码,而对于后者,你需要自己克隆代码。

你不应该使用相对于项目本身的路径,比如

import "./project/package"

这在所有的go工具中都不兼容,并且是不被推荐的。

英文:

These are exactly the same as far as the go build tools are concerned. The fact that package is in the directory

$GOPATH/src/github.com/owner/project/package

or in the directory

$GOPATH/src/project/package

makes no difference.

The only difference is that with the former you can use go get to fetch the source automatically, and the latter you have to clone the code yourself.

What you don't want to use are paths relative to the project itself, like

import "./project/package"

That is not compatible with all the go tools, and is highly discouraged.

huangapple
  • 本文由 发表于 2015年9月3日 22:40:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/32378725.html
匿名

发表评论

匿名网友

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

确定