Documentation & godoc Conventions
Package comments, examples, and internal playbooks.
Search across all documentation pages
Package comments, examples, and internal playbooks.
Go documentation is comments interpreted by go doc and pkg.go.dev.
Teams that treat docs as part of the API reduce onboarding friction, improve review quality, and make generated reference material trustworthy.
godoc is the default reference surface for Go packages.
Package comments explain purpose and usage patterns.
Symbol comments document behavior, concurrency rules, and error semantics.
Examples provide executable snippets.
Internal playbooks cover runbooks and onboarding steps godoc should not try to hold.
Quick-reference recipe card - copy-paste ready.
// Package ratelimit provides token-bucket rate limiting for HTTP handlers.
//
// Use NewLimiter at process startup and share one Limiter across handlers.
// Limiters are safe for concurrent use.
package ratelimit
// Limiter controls request rate using a token bucket.
type Limiter struct { /* ... */ }
// Allow reports whether one event may proceed at time now.
// It is safe for concurrent use by multiple goroutines.
func (l *Limiter) Allow(now time.Time) bool { /* ... */ }When to reach for this:
A library adds executable documentation and a package overview.
// example_test.go
package ratelimit_test
import (
"fmt"
"time"
"example.com/lib/ratelimit"
)
func ExampleNewLimiter() {
lim := ratelimit.NewLimiter(10, time.Second)
ok := lim.Allow(time.Now())
fmt.Println(ok)
// Output: true
}go test -run Example
go doc example.com/lib/ratelimitWhat this demonstrates:
*_test.go with Example prefix and optional // Output:.go test runs examples as tests; broken examples fail CI.Limiter methods.package clause (only one file per package should carry the main block).| Element | Rule |
|---|---|
| Exported func/type/const | Comment starts with name; states what it does |
| Errors | Document return conditions and retryability |
| Context | Note cancellation effects |
| Concurrency | State safe/unsafe explicitly |
| Zero value | Say whether zero value is useful |
| Deprecated | // Deprecated: use NewX instead |
// Bad - comment does not start with symbol name
// Returns a limiter for rate control.
func NewLimiter(rate int, window time.Duration) *Limiter
// Good
// NewLimiter returns a Limiter that allows rate events per window.
func NewLimiter(rate int, window time.Duration) *Limiter// ErrRateExceeded indicates the client exceeded quota.
// Callers may retry after RetryAfter duration.
var ErrRateExceeded = errors.New("rate exceeded")Link related types with plain text names; godoc auto-links identifiers when possible.
doc.go or designated file per package.httptest.Server with deterministic output.| Alternative | Use When | Don't Use When |
|---|---|---|
| README only | Internal cmd tools | Exported libraries consumed by other teams |
| OpenAPI for HTTP | REST surface is primary contract | Pure Go libraries with no HTTP |
| Protobuf comments | gRPC is main API | Hand-written Go client helpers still need godoc |
| Generated docs (swaggo) | Large HTTP surface | You still need package-level Go comments for logic |
Document unexported items when complexity demands it, but godoc will not show them on pkg.go.dev. Prefer clear names for internals.
One to three sentences for most symbols. Longer prose belongs in package comment or markdown design docs linked from README.
Test names and table cases are documentation for teammates. Use Example functions when you want pkg.go.dev snippets.
Add Deprecated: line with replacement and timeline. Reviewers block new uses of deprecated symbols in application code.
Yes when APIs need generics or new stdlib symbols. Align with module go directive in go.mod.
Match team policy. Mixed languages harm search and onboarding; most production codebases standardize on English for exports.
Run go test for examples, optionally go vet and linters that check comment sentences on exports.
Service owners rotate quarterly review of README and runbook links; platform teams own shared templates.
ADRs record why; godoc records what and how. Link ADRs in README, not in every function comment.
Authors must verify accuracy against code paths and errors. Reviewers treat wrong godoc as a blocking bug.
go doc exploration exerciseStack versions: This page was written for Go 1.26.x (Green Tea GC default, go fix modernizers - verify patch at build), chi (latest - verify at build), gin (latest - verify at build), echo (latest - verify at build), google.golang.org/grpc (latest - verify at build), sigs.k8s.io/controller-runtime (latest - verify at build), kubebuilder (latest - verify at build), tinygo (latest - verify board targets at build), wazero (latest - verify at build), and golangci-lint (latest - verify linter set at build).
Reviewed by Chris St. John·Last updated Jul 16, 2026