Directory layout and `got get …` for Go language

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

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)

huangapple
  • 本文由 发表于 2022年6月1日 16:15:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/72458468.html
匿名

发表评论

匿名网友

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

确定