英文:
Directory layout and `got get ...` for Go language
问题
假设我在目录project/
中有一个main.go
文件,并且有一个子目录project/pkg/mydb/
,main.go
使用了该子目录。
要向mydb/
目录中的代码添加依赖项,我应该在mydb/
子目录中运行go get ...
命令,还是在顶级项目目录(project/
)中运行?
另外,main.go
应该位于project/
目录中还是project/src/
目录中?
英文:
Suppose I have main.go
in directory project/
and have subdirectory project/pkg/mydb/
that is used by main.go
.
To add a dependency to my code inside mydb/
I should run go get ...
in that subdir mydb/
or in top-level project directory (project/
)?
Also where main.go
should reside: directly in project/
or in project/src/
?
答案1
得分: 2
只考虑模块和包。通常你的模块会是 project/
,可以由一个或多个包组成(project/pkg/mydb
可以是其中之一)。
只有 Go 模块才有依赖关系。所以你应该在 project
目录下运行 go get
。
main.go
可以放在任何你想要的地方,这只会改变你是否需要运行 go build .
还是 go build ./src
。
(这只适用于使用 Go 模块的情况,所以如果你的 project
中有 go.mod 文件的话,你应该使用它们)
英文:
Go only think in term of module and packages. Usually your module would be project/
and can be composed of one or several packages (project/pkg/mydb
can be one of them)
Only go modules have dependencies. So you should run go get
in project
main.go
can be wherever you want, it will just change whether you need to run go build .
or go build ./src
(This is only applicable if you use go modules, so if you have a go.mod in your project
. But if you should be using them anyway)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论