2.3 KiB
Project Structure
One of Go's strength's is its simplicity. Project structure should be kept simple as well.
A Common Misconception
Although the golang-standards/project-layout repo looks official (23k+ stars, "golang" in the name, etc), it is not. Russ Cox, a principal engineer at Google and the most active contributor to Go has opened an issue to address this.
How Complex is Your Project?
On the complex end of the spectrum, projects like CockroachDB and Kubernetes use a complex project structure. gorilla/mux is comparatively simple. It has no dependencies and has no directory structure. Here are complexity scores for these 3 projects, and a few others, using scc:
Project | Lines in .go Files | Complexity score |
---|---|---|
Kubernetes | 4,913,049 | 524,227 |
CockroachDB | 4,064,838 | 725,465 |
gitea | 223,761 | 39,001 |
hugo | 148,613 | 16,642 |
go-github | 117,131 | 15,637 |
traefik | 107,542 | 9,154 |
gio | 47,283 | 5,122 |
gorilla/mux | 6,661 | 763 |
The complexity scores are rough estimates, but are useful in realizing how complex your code is. Creating project structure is not a pure science. There is an art to knowing how to lay out a project based on its current state, and its trajectory.
If your project is simple, do not add unnecessary structure. This increases the cognitive load of anyone trying to use the project.
Organize Code by Responsibility
Package internal
Package pkg
Use Encapsulation Tools to Create Project Structure
Just as structs, funcs, files, packages, and modules are