在Go中,可以使用Cmd文件夹来组织项目文件。

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

Cmd folder for organizing project files in Go

问题

我已经搜索了一个关于在项目中组织Go文件的解决方案,并找到了这篇文章。我想知道在该文章的第二部分中使用的cmd文件夹的作用是什么。它是Go编译器的一个特殊关键词吗?另一方面,我在Go文档中阅读了一下,并没有提到cmd文件夹。

那么这个文件夹是干什么用的?在Go中,如何最佳地组织项目文件以支持源文件、项目二进制文件、第三方包和单元测试呢?

英文:

I have searched for a solution for organizing Go files in a project and found this post. I wonder what the role of the cmd folder is, that was used in the 2nd section of that post. Is it a magic word for the Go compiler?
On the other hand I was reading the Go documentation and there is nothing about a cmd folder in there.

So what about this folder? And what is the best practice for structuring project files in Go that support source files, projects binaries, 3rd party packages and unit tests.

答案1

得分: 12

那么关于这个 'cmd' 文件夹呢?

这篇文章已经明确说明了,总结一下:
> 1. 在Go语言中,它不是一个魔法或标准,只是一种约定。
> 2. 将多个二进制文件放在子文件夹中是不可能在根文件夹中实现的。
> 3. 通过将你的二进制文件视为客户端而不是应用程序的主机或门户,它可以引导你采用所谓的“库驱动开发”方式来构建程序。这种分离“有助于实现更清晰的抽象”和更常见的代码逻辑。

那么在Go语言中,如何最佳实践地组织项目文件以支持源文件、项目二进制文件、第三方包和单元测试呢?

我不确定最佳实践是什么。官方文档中有很多关于项目文件组织的提示。除了用于存放二进制文件的 cmd 文件夹之外,根据我的个人实践,我会:

  1. 将源文件分组放在 src 文件夹的子文件夹中,
  2. 将第三方包复制到 vendor 文件夹中,
  3. 将单元测试文件与目标源文件放在一起。

以下链接可能会有所帮助:

> 组织Go代码
> Go博客:组织Go代码
> Go博客:包名
> golang-standards/project-layout (*)

正如 @DiegoMendes 指出的,最后一个链接中的提案存在争议。你还应该查看该存储库的 issue 117,其中包含了很多有价值的信息。

英文:

So what about this 'cmd' folder?

The post has already made it clear, to summarise:
> 1. It's not a magic nor standard in Go. It's just a convention.
> 2. You can have multiple binaries when putting them into a sub-folder which is not possible in the root folder.
> 3. By making you taking your binary as a client instead of a host or portal of your application, it can drives you to the so-called 'library-driven-development' way of architecting your program. This separation 'helps you make a cleaner abstraction' and more common of you code logic.

And what is the best practice for structuring project files in Go that support source files, projects binaries, 3rd party packages and unit tests.

I'm not sure about the best practice. The official documents have many hints about the project files structuring. <del>On my personal practice, besides the cmd folder for the binaries, I

  1. Group source files into packages(sub-folders) inside the src folder,
  2. Copy 3rd party packages to the vendor folder;
  3. Place unit tests files side by side with its target source file.
    </del>

These links may be helpful:

> Organizing Go code
> The Go Blog: Organizing Go code
> The Go Blog: Package names
> golang-standards/project-layout (*)

As @DiegoMendes pointed out, there are controversy about proposals in the last link. You should also check the issue 117 of that repo which contains a lot of valuable information.

答案2

得分: 2

你引用的帖子的作者明确表示,Camlistore应用程序使他了解了cmd约定。

如果你查看Camlistore的源代码,你会注意到该项目使用了一种自定义的构建系统,即"make.go"。

cmd之所以特殊,只是因为Camlistore项目使用"go run make.go"来构建,而make.go知道如何构建cmd目录中的目标。或者更一般地说,只有当你使用将cmd视为特殊目录的构建系统时,cmd才是特殊的。

英文:

The author of the post you cite explicitly says the Camlistore application introduced him to the cmd convention.

If you look at the source code to Camlistore you will notice that project uses a custom system for building, namely "make.go".

cmd is special only because the Camlistore project uses "go run make.go" to build and make.go is aware of how to build targets in the cmd directory. Or in the more general case, cmd is special only if you use a build system that treats it as special.

huangapple
  • 本文由 发表于 2015年8月16日 03:05:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/32028287.html
匿名

发表评论

匿名网友

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

确定