31

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.

RickyA
  • 15,465
  • 5
  • 71
  • 95
amirhosseinab
  • 1,030
  • 2
  • 16
  • 29

2 Answers2

12

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. 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.

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.

lfree
  • 1,880
  • 3
  • 24
  • 39
  • 4
    `golang-standards/project-layout` should probably removed from your links. There is a long discussion going on in the issues about it's proposal which is not 'stardand'. for more info check [issue 117](https://github.com/golang-standards/project-layout/issues/117) in the repo – Diego Mendes May 13 '21 at 18:24
2

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.

RickyA
  • 15,465
  • 5
  • 71
  • 95
golliher
  • 1,377
  • 3
  • 12
  • 31
  • The Go `cmd` convention has nothing to do with camlistor's build. – Dave C Aug 16 '15 at 12:20
  • It was explicitly cited by the author of the post in the OP's question. As such is it not worth studying in trying to understand the cmd convention that author is promoting? I don't understand the down vote. – golliher Aug 16 '15 at 12:23
  • @golliher Thanks for your information about `cmd` folder, could you help me about the second part of my question - the best practice and common ways for organizing project files – amirhosseinab Aug 17 '15 at 17:52
  • 1
    @amirhosseinab You are welcome. I am glad if I was able to help. While I felt qualified to answer the first part, I am in fact still a learner myself and do not feel qualified to try and give you a general best practice for organizing Go projects. I am sorry. I suspect it is a subjective thing. For a CLI application I used a framework called https://github.com/spf13/cobra and so I followed its patterns and used Hugo's source code to help instruct me (https://github.com/spf13/hugo) if i were writing a Revel application, I suspect the organization would be different (http://revel.github.io). – golliher Aug 17 '15 at 18:03
  • This may have the answer to the second part of your question. http://stackoverflow.com/questions/9985559/organizing-a-multiple-file-go-project – golliher Aug 18 '15 at 09:59