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