Go Documentation
Resource Routing
scripts/check-docs.sh - Run when checking exported functions, types, methods, constants, and packages for missing doc comments.
scripts/check-docs-ast.go - Implementation helper invoked by check-docs.sh; patch this when changing documentation analysis behavior.
assets/doc-template.go - Use when starting a documented package or exported API.
references/CONVENTIONS.md - Read when documenting parameters, context behavior, concurrency safety, cleanup, errors, or named results.
references/EXAMPLES.md - Read when adding runnable examples or package examples.
references/FORMATTING.md - Read when formatting Godoc lists, paragraphs, links, and code blocks.
Doc Comments
Normative: All top-level exported names must have doc comments.
Basic Rules
- Begin with the name of the object being described
- An article ("a", "an", "the") may precede the name
- Use full sentences (capitalized, punctuated)
// A Request represents a request to run a command.
type Request struct { ...
// Encode writes the JSON encoding of req to w.
func Encode(w io.Writer, req *Request) { ...
Unexported types/functions with unobvious behavior should also have doc comments.
Validation: After adding doc comments, run bash scripts/check-docs.sh to verify no exported symbols are missing documentation. Fix any gaps before proceeding.
Comment Sentences
Normative: Documentation comments must be complete sentences.
- Capitalize the first word, end with punctuation
- Exception: may begin with uncapitalized identifier if clear
- End-of-line comments for struct fields can be phrases
Comment Line Length
Advisory: Aim for ~80 columns, but no hard limit.
Break based on punctuation. Don't split long URLs.
Struct Documentation
Group fields with section comments. Mark optional fields with defaults:
type Options struct {
// General setup:
Name string
Group *FooGroup
// Customization:
LargeGroupThreshold int // optional; default: 10
}
Package Comments
Normative: Every package must have exactly one package comment.
// Package math provides basic constants and mathematical functions.
package math
- For
main packages, use the binary name: // The seed_generator command ...
- For long package comments, use a
doc.go file
What to Document
Advisory: Document non-obvious behavior, not obvious behavior.
| Topic |
Document when... |
Skip when... |
| Parameters |
Non-obvious behavior, edge cases |
Restates the type signature |
| Contexts |
Behavior differs from standard cancellation |
Standard ctx.Err() return |
| Concurrency |
Ambiguous thread safety (e.g., read that mutates) |
Read-only is safe, mutation is unsafe |
| Cleanup |
Always document resource release |
— |
| Errors |
Sentinel values, error types (use *PathError) |
— |
| Named results |
Multiple params of same type, action-oriented names |
Type alone is clear enough |
Key principles:
- Context cancellation returning
ctx.Err() is implied — don't restate it
- Read-only ops are assumed thread-safe; mutations assumed unsafe — don't restate
- Always document cleanup requirements (e.g.,
Call Stop to release resources)
- Use pointer in error type docs (
*PathError) for correct errors.Is/errors.As
- Don't name results just to enable naked returns — clarity > brevity
Runnable Examples
Advisory: Provide runnable examples in test files (*_test.go).
func ExampleConfig_WriteTo() {
cfg := &Config{Name: "example"}
cfg.WriteTo(os.Stdout)
// Output:
// {"name": "example"}
}
Examples appear in Godoc attached to the documented element.
Quick Reference
| Topic |
Key Rule |
| Doc comments |
Start with name, use full sentences |
| Line length |
~80 chars, prioritize readability |
| Package comments |
One per package, above package clause |
| Parameters |
Document non-obvious behavior only |
| Contexts |
Document exceptions to implied behavior |
| Concurrency |
Document ambiguous thread safety |
| Cleanup |
Always document resource release |
| Errors |
Document sentinels and types (note pointer) |
| Examples |
Use runnable examples in test files |
| Formatting |
Blank lines for paragraphs, indent for code |
Related Skills
- Naming conventions: See go-naming when choosing names for the identifiers your doc comments describe
- Testing examples: See go-testing when writing runnable
Example test functions that appear in godoc
- Linting enforcement: See go-linting when using revive or other linters to enforce doc comment presence
- Style principles: See go-style-core when balancing documentation verbosity against clarity and concision
1---
2name: go-documentation
3description: Use when writing or reviewing documentation for Go packages, types, functions, or methods. Also use proactively when creating new exported types, functions, or packages, even if the user doesn't explicitly ask about documentation. Does not cover code comments for non-exported symbols (see go-style-core).
4allowed-tools: Bash(bash:*)
5metadata:
6 source: https://skillmd.com/skills/om-scogo/go-documentation
7---
8
9# Go Documentation
10
11## Resource Routing
12
13- `scripts/check-docs.sh` - Run when checking exported functions, types, methods, constants, and packages for missing doc comments.
14- `scripts/check-docs-ast.go` - Implementation helper invoked by `check-docs.sh`; patch this when changing documentation analysis behavior.
15- `assets/doc-template.go` - Use when starting a documented package or exported API.
16- `references/CONVENTIONS.md` - Read when documenting parameters, context behavior, concurrency safety, cleanup, errors, or named results.
17- `references/EXAMPLES.md` - Read when adding runnable examples or package examples.
18- `references/FORMATTING.md` - Read when formatting Godoc lists, paragraphs, links, and code blocks.
19
20---
21
22## Doc Comments
23
24> **Normative**: All top-level exported names must have doc comments.
25
26### Basic Rules
27
281. Begin with the name of the object being described
292. An article ("a", "an", "the") may precede the name
303. Use full sentences (capitalized, punctuated)
31
32```go
33// A Request represents a request to run a command.
34type Request struct { ...
35
36// Encode writes the JSON encoding of req to w.
37func Encode(w io.Writer, req *Request) { ...
38```
39
40Unexported types/functions with unobvious behavior should also have doc comments.
41
42> **Validation**: After adding doc comments, run `bash scripts/check-docs.sh` to verify no exported symbols are missing documentation. Fix any gaps before proceeding.
43
44---
45
46## Comment Sentences
47
48> **Normative**: Documentation comments must be complete sentences.
49
50- Capitalize the first word, end with punctuation
51- Exception: may begin with uncapitalized identifier if clear
52- End-of-line comments for struct fields can be phrases
53
54---
55
56## Comment Line Length
57
58> **Advisory**: Aim for ~80 columns, but no hard limit.
59
60Break based on punctuation. Don't split long URLs.
61
62---
63
64## Struct Documentation
65
66Group fields with section comments. Mark optional fields with defaults:
67
68```go
69type Options struct {
70 // General setup:
71 Name string
72 Group *FooGroup
73
74 // Customization:
75 LargeGroupThreshold int // optional; default: 10
76}
77```
78
79---
80
81## Package Comments
82
83> **Normative**: Every package must have exactly one package comment.
84
85```go
86// Package math provides basic constants and mathematical functions.
87package math
88```
89
90- For `main` packages, use the binary name: `// The seed_generator command ...`
91- For long package comments, use a `doc.go` file
92
93---
94
95## What to Document
96
97> **Advisory**: Document non-obvious behavior, not obvious behavior.
98
99| Topic | Document when... | Skip when... |
100|-------|-----------------|--------------|
101| Parameters | Non-obvious behavior, edge cases | Restates the type signature |
102| Contexts | Behavior differs from standard cancellation | Standard `ctx.Err()` return |
103| Concurrency | Ambiguous thread safety (e.g., read that mutates) | Read-only is safe, mutation is unsafe |
104| Cleanup | Always document resource release | — |
105| Errors | Sentinel values, error types (use `*PathError`) | — |
106| Named results | Multiple params of same type, action-oriented names | Type alone is clear enough |
107
108Key principles:
109
110- Context cancellation returning `ctx.Err()` is implied — don't restate it
111- Read-only ops are assumed thread-safe; mutations assumed unsafe — don't restate
112- Always document cleanup requirements (e.g., `Call Stop to release resources`)
113- Use pointer in error type docs (`*PathError`) for correct `errors.Is`/`errors.As`
114- Don't name results just to enable naked returns — clarity > brevity
115
116---
117
118## Runnable Examples
119
120> **Advisory**: Provide runnable examples in test files (`*_test.go`).
121
122```go
123func ExampleConfig_WriteTo() {
124 cfg := &Config{Name: "example"}
125 cfg.WriteTo(os.Stdout)
126 // Output:
127 // {"name": "example"}
128}
129```
130
131Examples appear in Godoc attached to the documented element.
132
133---
134
135## Quick Reference
136
137| Topic | Key Rule |
138|-------|----------|
139| Doc comments | Start with name, use full sentences |
140| Line length | ~80 chars, prioritize readability |
141| Package comments | One per package, above `package` clause |
142| Parameters | Document non-obvious behavior only |
143| Contexts | Document exceptions to implied behavior |
144| Concurrency | Document ambiguous thread safety |
145| Cleanup | Always document resource release |
146| Errors | Document sentinels and types (note pointer) |
147| Examples | Use runnable examples in test files |
148| Formatting | Blank lines for paragraphs, indent for code |
149
150---
151
152## Related Skills
153
154- **Naming conventions**: See [go-naming](../go-naming/SKILL.md) when choosing names for the identifiers your doc comments describe
155- **Testing examples**: See [go-testing](../go-testing/SKILL.md) when writing runnable `Example` test functions that appear in godoc
156- **Linting enforcement**: See [go-linting](../go-linting/SKILL.md) when using revive or other linters to enforce doc comment presence
157- **Style principles**: See [go-style-core](../go-style-core/SKILL.md) when balancing documentation verbosity against clarity and concision