| name | godoc-comments |
| description | Write proper Go documentation comments for packages, types, funcs, consts, and vars. Use when writing, reviewing, or improving godoc comments. Covers naming conventions, syntax (headings, links, lists, code blocks), and common mistakes. |
| argument-hint | [identifier or file] |
Go Doc Comments
Write documentation comments that render correctly in go doc, pkg.go.dev, and
IDEs. Every exported (capitalized) name should have a doc comment.
Quick Reference
| Declaration | First Sentence Pattern | Example |
|---|
| Package | "Package <name>..." | // Package path implements utility routines... |
| Command | "<Name>..." (capitalized) | // Gofmt formats Go programs. |
| Type | "A <Name>..." or "An <Name>..." | // A Reader serves content from a ZIP archive. |
| Func (return) | "<Name> returns..." | // Quote returns a double-quoted Go string literal. |
| Func (action) | "<Name> causes/does..." | // Exit causes the current program to exit... |
| Func (boolean) | "<Name> reports whether..." | // HasPrefix reports whether s begins with prefix. |
| Const group | Describe the set | // The result of Scan is one of these tokens... |
| Single const | Complete sentence | // Version is the Unicode edition... |
Package Comments
Place in one source file per package (they concatenate if in multiple files).
package path
Type Comments
Explain what instances represent. Document concurrency safety if stronger than
"single goroutine at a time." Document zero value meaning if not obvious.
type Buffer struct { ... }
type Regexp struct { ... }
For structs with exported fields, explain fields in doc comment or per-field:
type LimitedReader struct {
R Reader
N int64
}
Func Comments
Focus on what the caller needs to know: return values or side effects.
Document special cases explicitly.
func Sqrt(x float64) float64
func Copy(dst Writer, src Reader) (n int64, err error)
func (c *Conn) Close() error
Do NOT document internal algorithms—those go in code comments.
Const and Var Comments
Grouped declarations can share a comment:
var (
ErrInvalid = errInvalid()
ErrPermission = errPermission()
ErrExist = errExist()
)
const (
EOF = -(iota + 1)
Ident
Int
Float
)
Doc Comment Syntax
Headings
Use # prefix (Go 1.19+):
package strconv
Links and Doc Links
[text] — link to exported identifier in current package
[pkg.Name] — link to identifier in another package
[text]: URL — define external URL (goes at end of comment)
Lists
Indent with spaces. Bullet lists use -. Numbered lists use 1., 2..
func Clean(path string) string
type PublicSuffixList interface { ... }
Code Blocks
Indent with tab. Insert blank lines before and after.
func Search(n int, f func(int) bool) int
Common Mistakes
Unindented Lists Become Code Blocks
Wrong:
Renders as: "Features:" paragraph, then "1) First feature. 2) Second feature."
paragraph, then a code block with the continuation.
Correct:
Indented Continuation Lines Become Code Blocks
Wrong:
The indented second line becomes a code block.
Correct:
Nested Lists Not Supported
Go doc comments do not support nested lists. Rewrite to avoid nesting or mix
list markers (numbered outer, bullet inner).
Deprecation Notices
Start paragraph with Deprecated::
func (c *Cipher) Reset()
Notes
Use MARKER(uid): format for TODO, BUG, etc:
var ctx context.Context
Directive Placement
Directives like //go:generate go at the end of the doc comment, after a blank
line:
type Op uint8