Skip to content

Code Style

go-zero follows standard Go conventions with a few additional guidelines.

All code must be formatted with gofmt / goimports:

Terminal window
goimports -w ./...

CI will reject PRs with formatting issues.

ElementConventionExample
Packageshort, lowercase, no underscoreslogx, httpx
Interfacedescriptive noun or -er suffixUserModel, Breaker
ConstructorNewXxx or MustNewXxxNewCache, MustNewServer
Error varsErrXxxErrNotFound, ErrTimeout
Config structsembed standard baseRestConf, RpcServerConf
  • Never ignore errors silently.
  • Wrap errors with context: fmt.Errorf("createUser: %w", err).
  • Use sentinel errors defined at package level for type assertions.
  • Unit tests alongside source: foo_test.go in the same package.
  • Table-driven tests preferred.
  • Mocks generated via mockgen or hand-written minimal stubs.
  • Minimum coverage target: 80% for new packages.
Terminal window
go test -race -coverprofile=coverage.out ./...
go tool cover -html=coverage.out
  • All exported symbols must have a doc comment.
  • Comments complete sentences, starting with the symbol name.
  • Non-obvious logic gets an inline comment explaining why.