4 ms·
Hmm, interesting I had assumed /cmd/ was used for cli-ish applications that use a 'command pattern'. I didn't think people also did this for http apis.
by stevehiehn 7y ago
Hmm, interesting I had assumed /cmd/ was used for cli-ish applications that use a 'command pattern'. I didn't think people also did this for http apis.
- divan 7y agoIt really depends on the type of the project. You generally use `cmd` package if you have more than 2 binaries in your project. Or, in rare case, where you ship a library and a binary (so you still can import a library by root package path). Otherwise, you don't need cmd/ dir at all.
- tptacek 7y agoThat is my understanding as well, and having a directory tree that goes like "projectname/cmd/projectlibrary" seems super weird and non-idiomatic to me.
- herval 7y agopkg/cmd or just /cmd are rather popular in a lot of projects (https://github.com/grafana/grafana/tree/master/pkg/cmd https://github.com/grafana/grafana/tree/master/pkg/cmd / https://github.com/prometheus/prometheus/tree/master/cmd https://github.com/prometheus/prometheus/tree/master/cmd / https://github.com/istio/istio/tree/c3e1824c5e3f3e51ceb4a80b46aee9876be07131/istioctl/cmd https://github.com/istio/istio/tree/c3e1824c5e3f3e51ceb4a80b... / https://github.com/containous/traefik/tree/master/cmd https://github.com/containous/traefik/tree/master/cmd / etc)
- tptacek 7y agocmd/ directories are normal. Hosting all the business logic for the whole package under cmd/ seems less normal. You kind of need cmd/, because you can only have one main() in a package, and a package with a main() can't easily be imported into another package. That's why the directory exists, not (as I understand it) because there is some merit to burying code 3 directory levels deep.
- herval 7y agomy response was to your comment that "having a directory tree that goes like "projectname/cmd/projectlibrary" seems super weird and non-idiomatic to me"
- PhilippGille 7y agoI'm on mobile and didn't check all your examples, but grafana has pkg/cmd and traefik has cmd next to pkg. That's not having the business logic below cmd. Whereas cmd/pkg is.
- pixelrevision 7y agoMost projects I see put source at the root so it’s straightforward to import and if there is a commandline utility you can build it ends up in ./cmd/main.go or something. One thing I dislike about go is that the way imports work you often see source code mixed with stuff like docker and make files.
- sagichmal 7y agoNever cmd/main.go, always cmd/binaryname/main.go.