Go organises code with modules and packages. A module is a tree of packages with a go.mod file at its root, and a package is a directory of .go files that share a package name. Keep a small, standard layout, use go mod to manage dependencies, format and vet with the standard tools, and let tests, linters and CI protect the code as it grows.
Go has strong conventions rather than a framework. This article covers a practical project layout, dependency management, workspaces, testing, tooling and release practices for libraries, command line tools and services.
Create a module with go mod init, using the path where the code will be hosted:
The generated go.mod records the module path, the Go version and dependencies:
Useful commands:
| Command | Purpose |
|---|---|
go get example.com/[email protected] | Add or change a dependency |
go mod tidy | Add missing and remove unused requirements |
go mod download | Fetch modules into the cache |
go mod vendor | Copy dependencies into vendor/ |
go list -m all | List all modules in the build |
go list -m -u all | Show available upgrades |
go mod why -m example.com/pkg | Explain why a module is needed |
Commit both go.mod and go.sum. The go.sum file holds checksums that make builds verifiable. Since Go 1.21 the go line is a minimum required version and the toolchain line can pin a specific toolchain, which Go downloads automatically when needed. Since Go 1.24, a tool directive in go.mod tracks developer tools your project runs with go tool.
There is no official layout, but the Go team recommends starting simple and growing only when needed.
Guidelines:
cmd or internal until you need them.cmd/<name>/main.go and keep main thin: parse flags, wire dependencies, call into internal.internal/: only code inside the parent of internal can import it. Use it for anything you do not want to promise as public API.order, storage), not util or common.Go modules use semantic versioning with minimal version selection, which picks the lowest version that satisfies all requirements and makes builds reproducible. Major versions 2 and above add a suffix to the module path, for example example.com/lib/v2.
Good habits:
go mod tidy before every commit.go get -u ./... and run the tests.govulncheck ./....replace directives only for local development, and never leave them in released modules.GOPRIVATE variable for internal repositories.For work across several modules at once, use a workspace. go work init ./api ./worker creates a go.work file that lets the modules see each other's local changes. Do not commit go.work for libraries unless your team agrees on it.
Go removes style debates with tooling:
gofmt is mandatory, go vet finds suspicious constructs, and staticcheck or golangci-lint add more checks. Run them in a pre-commit hook and in CI.
Place tests next to the code in _test.go files. Use table-driven tests for many cases:
Useful flags are go test -race, -cover and -count=1. Put fixtures in testdata/, which the Go tool ignores. Add Example functions so documentation is also tested.
CGO_ENABLED=0 go build -o bin/shopd ./cmd/shopd, and cross compile with GOOS and GOARCH.-ldflags "-X main.version=1.2.3", or read module information with runtime/debug.ReadBuildInfo.log/slog for structured logs.v1.2.0. Library users fetch tags through the module proxy.go doc.Keep packages small and cohesive, review exported API carefully because it becomes a compatibility promise, and deprecate with a // Deprecated: comment paragraph. Run go fix after toolchain upgrades when it applies, update the Go version regularly because only the two latest releases get security fixes, and keep a changelog.
No. It is a convention some projects use for public libraries, but many do not. Start without it.
The compiler prevents code outside the parent tree of internal from importing it, so you can refactor it freely.
Usually not. Vendoring is useful for offline or audited builds; otherwise the module cache and checksum database are enough.
One unless parts are released independently. Use a workspace when you do need several.
Organise Go projects around a single module, thin cmd entry points and internal packages. Manage dependencies with go mod tidy, check vulnerabilities, and rely on gofmt, go vet, tests and CI to keep the code healthy as it evolves.