Golang仓库的正确结构

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

Correct structure of Golang repository

问题

我正在开始学习Golang开发。现在我的老板给了我一个由其他开发人员创建的项目仓库,现在他已经离开公司了,我无法向他询问相关问题。我对他推送到仓库的项目结构感到困惑,结构如下:

|-MyApp
|--bin
|--pkg
|--src
|----api(应用程序的代码)
|----github.com
|----golang.org
|----gopkg.in

对我来说,这与Go的结构完全一样,1.- 在仓库中应该只有api文件夹吗?
如果我进入api文件夹并运行go run main.go,我会收到一条消息,说找不到一些包,即使它们在文件夹中,2.- 我如何在go run命令中指定包?

3.- 对于Golang项目来说,设置这种结构是一个好的实践吗?我在代码中看到他只使用"package1"导出包,如果我将应用程序的代码复制粘贴到Golang工作区中,那么我必须指定文件夹的名称来导出包,例如:"myApp/package1",所以我有这个疑问。谢谢。

英文:

I am starting in Golang development. Right now my boss gave me the repository of a project that other developer made, now he's gone of the company and I am not able to ask him some things related to it. Right now I have a confussion about the project structure that he pushed to the repo, the structure is the next:

 |-MyApp
 |--bin
 |--pkg
 |--src
 |----api (the code of the app)
 |----github.com
 |----golang.org
 |----gopkg.in

To me, it's exaclty as the estructure of the Go, 1.- in the repo should not be only the api folder?
If I go to the api folder and make go run main.go I get a message that some packages are not found even when they are in the folder, 2.-how I specify the packages in the go run command?

3.- Is a good practice to set this kind of structure for the golang projects? I see in the code that he exported the packages only with "package1", if I copy and paste the code of the app inside the golang workspace then I have to specify the name of the folder to export the packages, example: "myApp/package1" so there I have that doubt. Thank you

答案1

得分: 2

这完全取决于情况。并没有一种适用于所有情况的唯一正确方式。

看起来这个仓库决定将所有内容都放入vendor文件夹,整个GOPATH。这样做也可以,但是使用现代的"vendor"文件夹,可能会有不同的做法。

永远不要使用"go run"。那只适用于玩具程序。使用"go build"和"go install"来构建你的软件。这些命令适用于包路径。

对于其他所有情况,请参阅官方文档https://golang.org/doc/code.html,并遵循其中的指导(这意味着你可能需要稍微调整一下代码结构)。

英文:

That all depends. There is not a single right way for everything.

It seems as if this repo decided to vendor everything, the whole GOPATH. That is okay but with the "modern" vendor folder today one would do it probably differently.

Never do go run. That's for toy programs only. Build your software with go build and go install. These command work on package path.

For everything else: Please see the official documentation https://golang.org/doc/code.html and stick to it (which means you should move stuff around a bit).

huangapple
  • 本文由 发表于 2017年6月24日 22:34:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/44737505.html
匿名

发表评论

匿名网友

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

确定