Go documentation is written as ordinary comments placed directly above a declaration, starting with the name of the thing being documented. The go doc command and pkg.go.dev read those doc comments, so one comment serves developers in the editor, in the terminal and on the web. Comments inside function bodies are separate: they explain why code does something, not what the API is.
Following the conventions below lets you document a program for several audiences at once: users of the package, maintainers, and reviewers.
A doc comment is a // comment block immediately before a package, constant, variable, type or function, with no blank line between them. The first sentence should be a complete sentence starting with the declared name, because tools show it as the summary.
Rules that matter in practice:
// Add returns ..., not // This function adds ....doc.go for long text.Deprecated: so tools and linters flag them.Go doc comments support light formatting without HTML or Markdown headings. gofmt reformats them to the canonical layout (since Go 1.19).
| Element | How to write it |
|---|---|
| Paragraph | Separate with a blank // line |
| Heading | A line starting with # after a blank // line |
| Code block | Indent the lines by a tab or spaces |
| List | Lines starting with -, * or 1. |
| Link to another symbol | [Name] or [pkg.Name] |
| Link to a URL | [text] plus a [text]: https://... line at the end |
The older habit of writing // Example: followed by non-indented code does not render as code. Indent the code by one tab after the // instead.
The best usage example is one the compiler and test runner check. Put it in a _test.go file in a function named ExampleName, and add an // Output: comment.
go test runs it and fails if the printed output differs. pkg.go.dev shows it under the Add function with a Run button. Use ExampleType_Method to attach an example to a method and a suffix such as ExampleAdd_negative for variants.
The godoc web server is no longer the standard tool. Use go doc in the terminal and pkg.go.dev for published modules.
pkg.go.dev builds documentation from the doc comments of any public module you publish with a version tag, so you do not need to host anything. For a private module, pkgsite from the Go team can serve the same view locally. The official reference is the Go Doc Comments page at https://go.dev/doc/comment.
Inline comments are for readers of the implementation. Explain intent, constraints and surprising choices, not what the syntax already says.
Avoid comments like // increment i. Use // TODO(name): ... for follow-up work and //go:generate and other //go: lines only as directives, with no space after the slashes. Prefer // over /* */ for everything except package comments you want to keep as one block.
| Audience | Where to write |
|---|---|
| Package users | Doc comments and examples on exported names |
| Contributors | README.md, CONTRIBUTING.md, doc.go for design notes |
| Maintainers | Inline comments explaining why, plus tests |
| Operators | Command help text, README sections on config and flags |
Every exported one, yes. Unexported functions need a comment only when their purpose or constraints are not clear from the name and body.
There is probably a blank line between the comment and the declaration, or the comment is attached to an unexported name. Put it directly above an exported declaration.
Only a small subset: headings with #, lists, links with [text], and indented code blocks. Bold and tables are not supported.
The standalone godoc tool is deprecated for most uses. Use go doc, pkg.go.dev, or pkgsite instead.
Write doc comments directly above exported declarations, begin them with the declared name, and use the supported light formatting. Add runnable Example functions in test files, read results with go doc and pkg.go.dev, and keep inline comments for explaining why the code is the way it is.