Designing an Effective Go Web Project Directory Structure
This article explains the principles and recommended layout for organizing a large Go web project, covering readability, extensibility, standard Go project layout, and detailed descriptions of directories such as /cmd, /internal, /pkg, /configs, /deployments, and supporting files to improve maintainability and scalability.
Go, known for its efficiency, simplicity, and safe concurrency, is increasingly popular for web and cloud‑native development, and a well‑designed directory structure is essential for large Go web projects to improve code organization, maintainability, and scalability.
Basic Principles
Before designing a project layout, consider readability and maintainability, extensibility and modularity, and consistency; directory names should be concise, clear, and preferably singular, with occasional exceptions such as /docs and /examples .
Go Project Standard Layout
The Go community’s golang-standards/project-layout provides a functional, convention‑based structure that promotes consistency across projects.
Go Application Directories
/cmd
Contains entry‑point files like main.go for each component; keep this directory free of business logic.
cmd
├── ctl
│ └── main.go
├── server
│ └── main.go
└── task
└── main.go/internal
Holds private code and libraries that cannot be imported by external projects; Go enforces this at compile time.
use of internal package xxx not allowedSub‑directories such as /internal/app for component logic and /internal/pkg for shared internal code are common.
/pkg
Exports public libraries that may be imported by other projects; use cautiously to avoid exposing private code.
/configs
Stores configuration file templates or defaults; the plural form is a de‑facto convention.
/test
Used for end‑to‑end tests and test data, with optional sub‑directories like /test/data or /test/testdata . Directories starting with . or _ are ignored by Go.
/deployments
Contains IaaS/PaaS and container orchestration assets (Docker‑Compose, Helm, Terraform, etc.). For Kubernetes projects, /deploy is preferred.
/third_party
Holds external tools, forks, or third‑party UI assets.
/web
If the project includes a frontend, place static assets, frontend code, and routing here; otherwise, keep the frontend separate.
Project Management Directories
/init
System initialization and process‑management configurations (systemd, supervisord, etc.) and initialization code such as database migrations.
/scripts
Scripts for building, installing, or analyzing the project; the root /Makefile can invoke them.
/build
Files for building and CI; sub‑directories like /build/package for package configs and /build/ci for CI pipelines.
/tools
Support tools that import code from /pkg and /internal .
/assets
Other resources such as images, CSS, JavaScript, or SQL files.
/githooks
Git hook scripts.
Documentation Directories
/api
External API specifications (OpenAPI/Swagger, JSON Schema, protobuf, etc.).
/docs
Design, development, and user documentation (excluding godoc output).
/examples
Example applications or library usage.
Directories to Avoid
/src
Common in Java/Python projects but discouraged in Go; using $GOPATH/src already provides the necessary workspace.
Summary
The final recommended layout combines the standard Go project layout with practical additions, resulting in a comprehensive tree that includes CHANGELOG, CONTRIBUTING.md, LICENSE, Makefile, README.md, and directories such as api, assets, build, cmd, configs, deployments, docs, examples, githooks, init, internal, pkg, scripts, test, third_party, tools, and web. This structure helps developers manage code effectively, enhances maintainability, and scales well for medium to large projects.
Go Programming World
Mobile version of tech blog https://jianghushinian.cn/, covering Golang, Docker, Kubernetes and beyond.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.