| name | go-documentation-standards |
| description | Reference knowledge base for the go-doc-comments agent. Loaded by that agent on its first iteration when the host has not already injected it; not intended for direct invocation. |
Go Documentation Standards
A comprehensive guide to Go documentation comments following the official "Go Doc Comments" specification and community best practices. This document serves as the knowledge base for the go-doc-comments pattern.
Table of Contents
- Core Principles
- Syntax by Declaration Type
- Modern Doc Comment Features
- What to Document
- Common Mistakes
- Quality Checklist
Core Principles
The Golden Rule
Doc comments appear immediately before top-level declarations with no intervening blank lines. All exported (capitalized) names that carry non-obvious meaning must have doc comments. Self-documenting names (String, Len, simple getters, trivial New) are intentionally left undocumented — a comment that only restates the identifier is a defect, not a fix.
Philosophy
| Principle | Description |
|---|
| Complete sentences | Start with the declared name |
| Explain what | Not how it works internally |
| User focus | Focus on user needs, not implementation |
| Searchable | Clear, explicit, searchable text |
| No redundancy | Avoid "ProcessData processes the data" |
| Add value | Provide info beyond the function signature |
Line Length
All comment lines should stay within 80 characters for readability.
Syntax by Declaration Type
Package Comments
package regexp
Rules:
- Start with "Package [name]"
- Only include in ONE file of multi-file packages
- Directly adjacent to package clause (no blank line)
- For commands, describe program behavior
Functions and Methods
func Join(paths ...string) string
func (f *File) Open() bool
Rules:
- Start with function/method name
- For boolean returns: "reports whether [condition]" (omit "or not")
- Reference parameters/results without special syntax
- Describe return values and side effects
Boolean Functions:
func (c *Config) IsValid() bool
func Contains(s, substr string) bool
Types
type Request struct {
Method string
URL *url.URL
}
Rules:
- Use "A [Type] represents..." or "[Type] is..."
- Document concurrency safety if relevant
- Explain zero value behavior if non-obvious
- Document exported fields (in type comment or per-field)
Constants and Variables
Grouped constants:
const (
MaxSize = 1024 * 1024
StatusOK = 200
StatusError = 500
)
Ungrouped:
var ErrNotFound = errors.New("not found")
Rules:
- Grouped: single doc comment + end-of-line comments
- Ungrouped: full doc comments with complete sentences
Modern Doc Comment Features
Headings (Go 1.19+)
Use # (with space) on single unindented line, surrounded by blank lines:
Doc Links (to Go identifiers)
Syntax:
| Pattern | Description |
|---|
[Name] | Local identifier |
[Name.Method] | Method of local type |
[pkg.Name] | Identifier in another package |
[*Type] | Pointer to type |
URL Links
Lists
Bullet lists (indent 2 spaces before marker, 4 for continuation):
Numbered lists:
Rules:
- Use
- for bullets (gofmt normalizes)
- No nested lists (not supported)
- Only paragraphs allowed in lists
Code Blocks
Indent lines that aren't list markers:
What to Document
Concurrency Safety
Document when non-obvious or when stronger guarantees than default:
type Cache struct { ... }
type Buffer struct { ... }
Error Values
var ErrTimeout = errors.New("operation timeout")
Cleanup Requirements
func (f *File) Close() error
Context Behavior
Document when non-standard:
func (c *Command) Execute(ctx context.Context) error
Parameter Constraints
func NewClient(timeout time.Duration, retryCount int) *Client
Return Values
func Find(text, pattern string) int
Side Effects
func (s *Server) Shutdown(ctx context.Context) error
Common Mistakes
Redundant Comments
Bad:
func Process(data []byte) error
Good:
func Process(data []byte) error
Best (when the name already says it all): add no comment. For
self-documenting names like String, Len, or a simple getter, leaving
the declaration undocumented is correct — list it as trivial in the
skipped table. A lateral rewrite of an already-adequate comment is not an
improvement either; leave it alone.
Implementation Details
Bad:
func GetUser(id int) (*User, error)
Good:
func GetUser(id int) (*User, error)
Missing Declared Name
Bad:
func Now() time.Time
Good:
func Now() time.Time
Improper Indentation
Bad (breaks list):
Good:
Blank Line Between Comment and Declaration
Bad:
func GetUser(id int) (*User, error)
Good:
func GetUser(id int) (*User, error)
Special Syntax
Deprecation
func OldClient() *Client
Start paragraph with Deprecated: - tools warn on usage.
TODO/BUG/FIXME
Format: MARKER(uid): description
Directives
type Op uint8
Rules:
- gofmt moves directives after blank line at end of comment
- Not part of godoc output
- Common directives:
//go:generate, //go:embed, //go:build
Tools and Automation
gofmt
Automatically formats doc comments:
- Normalizes list indentation (converts
*, +, • to -)
- Converts backticks and quotes to Unicode
- Removes common indentation prefix
- Adds blank lines around code blocks
- Moves link definitions and directives to end of comment
gofmt -w .
go doc
View formatted documentation in terminal:
go doc pkgname
go doc pkgname.TypeName
go doc pkgname.FuncName
godoc-lint
Linter for doc comment consistency (validates against Go Doc Comments spec):
go install golang.org/x/tools/cmd/godoc-lint@latest
godoc-lint ./...
pkg.go.dev
Online documentation rendering at https://pkg.go.dev - automatically extracts and renders doc comments for public packages.
Signal Boosting Comments
From Google Style Guide: add clarifying comments for unusual patterns that might confuse readers.
Example - unusual condition:
if err == nil {
return cleanup()
}
When to add signal boosting:
- Unusual control flow (checking for success instead of failure)
- Non-obvious side effects
- Intentionally empty blocks
- Counter-intuitive logic
Quality Checklist
Before Submitting
Common Patterns
| Declaration | Pattern |
|---|
| Function returning value | "[Name] returns..." |
| Function returning bool | "[Name] reports whether..." |
| Function with side effects | "[Name] [action]s..." |
| Type | "A [Type] represents..." or "[Type] is..." |
| Error variable | "[Name] is returned when..." |
| Constant | "[Name] is the..." |
References
Last updated: 2026-01-10