In Go, you document a program with doc comments, which are plain // comments placed right above a package, type, function, constant or variable and starting with its name. Tools such as go doc, gopls hover text and pkg.go.dev turn those comments into browsable documentation. Regular inline comments inside functions explain design decisions and tricky implementation details.
Good Go documentation answers two questions: what does this do for callers, and why is it built this way. Doc comments handle the first, inline comments and package-level notes handle the second.
Document each exported identifier with a complete sentence that begins with the name.
The comment sits directly above the declaration with no blank line. State the behavior callers rely on: error conditions, nil handling, concurrency safety and units. You do not need to repeat parameter types, because the signature already shows them.
Every package should have one package comment, placed before the package line. For package main, describe the command instead.
For long overviews, put the comment in a file named doc.go so it does not crowd the code.
The go doc command prints documentation in the terminal. The old godoc -http server is deprecated, and pkg.go.dev now serves public modules.
When you publish a module to a public repository with a version tag, pkg.go.dev picks it up and renders the same comments, along with examples. The reference for format rules is https://go.dev/doc/comment.
Inside a function, comment on intent and non-obvious constraints. A comment that restates the code is noise and tends to become wrong when the code changes.
Naming the constant TaxRate is better than the original pattern of writing // Tax rate is 15% next to a literal 0.15, because the value and its meaning live in one place.
Go style prefers // line comments. Block comments /* ... */ are mostly used for long package comments or for temporarily disabling code. Most formatters and linters expect //.
| Comment | Meaning |
|---|---|
// TODO(name): ... | Work to do later, with an owner |
// Deprecated: ... | Marks an API as deprecated; place it as its own paragraph in the doc comment |
//go:build linux | Build constraint (a directive, no space after //) |
//go:generate cmd args | Command run by go generate |
An example function in a _test.go file is both documentation and a test.
go test verifies the output, and pkg.go.dev shows the example next to Add. If the API changes and the example breaks, the build tells you, which keeps the documentation honest. The original idea of embedding sample code in prose as result := calculator.Add(5, 3) is fine for text, but an Example function is better because it is checked.
Doc comments describe what callers see. For design and implementation reasoning, use:
doc.go or package comment section describing the overall approach and its limits.README.md or architecture note for system-level context.TestDivideByZeroReturnsError.Keep these close to the code so they change when the code changes.
A doc comment is attached to a declaration, immediately above it, and is extracted by go doc and pkg.go.dev. A normal comment inside a function body is not extracted.
Yes. By convention it does, for example // Add returns the sum.... This makes search and tool output read naturally.
Only when they are complex or their purpose is not clear. Exported names must be documented, and linters such as revive can enforce that.
In a comment directly above the package clause in one file of the package, often doc.go.
Write doc comments for every exported name, starting with the name and describing behavior and errors. View them with go doc and pkg.go.dev, add runnable examples in test files, and use inline comments to explain why, not what. Keep design notes near the code and name constants instead of explaining magic numbers.