godoc "comment formatting is not canonical" error
Fixes the 'comment formatting is not canonical' finding from gofmt-aware linters. Shows how to list affected files, preview gofmt's doc-comment normalization, apply it, and re-run the linter. Use when a linter flags non-canonical Go doc comments; the canonical-formatting message is the key trigger.
TL;DR: Run gofmt -w on the file. Since Go 1.19, gofmt normalizes doc comments (list indentation, code block indentation, link formatting), and linters flag comments that are not in that canonical form. One gofmt pass rewrites them; then re-run the linter.
comment formatting is not canonical- List the affected files:
gofmt -l .Expected: the file with the bad comment is listed.
- Preview what gofmt will change in the comments:
gofmt -d path/to/file.goExpected: a diff showing only comment reformatting (indentation, list markers).
- Apply it:
gofmt -w path/to/file.go- Verify:
gofmt -l .Expected: empty output, meaning every file is now canonical.
- Re-run the linter that reported the finding. Expected: the canonical-formatting findings are gone.
Use this when
- A linter reports "comment formatting is not canonical"
gofmt -llists files you thought were formatted- Doc comment lists or code examples render oddly on pkg.go.dev
Not for this skill when
- The complaint is about comment content (missing docs, bad grammar) - gofmt only fixes formatting
- The file has a syntax error gofmt cannot parse - fix the syntax error first
- You are on Go older than 1.19 - doc comment reformatting did not exist yet
Variant phrasings
- "gofmt comment formatting is not canonical"
- "go vet doc comment formatting"
- "golangci-lint gofmt canonical comment"
Why it happens
Go 1.19 taught gofmt to reformat doc comments into a canonical shape: indented code blocks, consistent list markers, and standard link syntax. Comments written before that, or by editors that do not run gofmt, keep the old shape and get flagged.
Edge cases
- gofmt only reformats; it never changes the words, so content lints need separate fixes.
- Run gofmt on the whole repo in CI so one canonical pass prevents repeat findings.
- Doc links use the bracket-text plus link-definition style; gofmt normalizes the definitions to the bottom of the comment.
Provenance
Resolved from the public thread: https://vectle.com/posts/pst_bwMPeLglBbs9wM1oScMkTg
Maintainer review
No maintainer verification is recorded for this version.
This records the version a maintainer checked. It does not assert that the version is the latest upstream release.