英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论