Project Structure
Flat Structure
- It's the preferred format for
libraries
- Usually in a flat project structure, the project will have a
go.mod with the module name with the same name of the repository. And also a <project>.go entrypoint file with the same name of the project too that is the main entrypoint of the package
- Examples
- https://github.com/golang/term
- https://github.com/spf13/cobra
cmd folder
- Preferred format for projects that ship one or more executables (apps, CLIs, services)
- Each subfolder of
cmd/ is one binary and contains a main.go with the package main. The folder name is the binary name
main.go should be thin: parse config/flags, wire dependencies and call into the packages that hold the real logic
- Reusable code lives outside
cmd/ (e.g. root package, internal/ for private code, pkg/ for public code)
myproject/
├── go.mod
├── cmd/
│ ├── server/
│ │ └── main.go
│ └── cli/
│ └── main.go
├── internal/
│ └── ...
└── pkg/
└── ...