Go go/format: format Go source in canonical gofmt style
Answer: Package format implements standard formatting of Go source.
When to call this package
gofmt output changes over time, so tools that need a stable layout should run a pinned gofmt binary instead of importing this package.
Tools that rely on consistent formatting should run a pinned gofmt binary rather than this package.
Pre-submit checks that call the package directly can differ across developers' Go versions.
Node writes canonical gofmt style for a node to an io.Writer.
Node, Source, and imports
The node type must be an ast File, a printer CommentedNode, a slice of ast Decl, or a slice of ast Stmt. It may also be assignment-compatible to ast Expr, ast Decl, ast Spec, or ast Stmt.
Partial source files do not get their imports sorted.
Source expects syntactically correct Go, or a list of declarations or statements.
Node makes a copy of the AST because ast.SortImports is destructive.
See also
Claims
- id: C1 claim: The package implements standard Go source formatting. source: S1 evidence: "Package format implements standard formatting of Go source."
- id: C2 claim: Node writes a node in canonical gofmt style. source: S1 evidence: "Node formats node in canonical gofmt style and writes the result to dst."
- id: C3 claim: Partial source files do not have imports sorted. source: S1 evidence: "Imports are not sorted for partial source files."
- id: C4 claim: The AST is copied because SortImports is destructive. source: S2 evidence: "Make a copy of the AST because ast.SortImports is destructive."
Sources
- [S1] go/format. The Go Authors. https://pkg.go.dev/go/format . License: BSD-3-Clause. Accessed 2026-11-26.
- [S2] go/format source. The Go Authors. https://go.dev/src/go/format/format.go . License: BSD-3-Clause. Accessed 2026-11-26.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: The Go Authors, go/format; The Go Authors, go/format source. BSD-3-Clause (https://go.dev/LICENSE). Copyright The Go Authors. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: The Go Authors, go/format; The Go Authors, go/format source. BSD-3-Clause (https://go.dev/LICENSE). Copyright The Go Authors.
Cite this
iinventedlight. 2026. "Go go/format: format Go source in canonical gofmt style." iinventedlight Library, iil:note:hxg010z9, version of 2026-11-26. https://iinventedlight.com/library/notes/go-format/. License: CC-BY-4.0.
@misc{iil_note_hxg010z9,
author = {{iinventedlight}},
title = {{Go go/format: format Go source in canonical gofmt style}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-26},
url = {https://iinventedlight.com/library/notes/go-format/},
note = {Permanent ID iil:note:hxg010z9. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
Extra files beside this note
These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.
- go-format.schema.json (schema; CC-BY-4.0)
- go-format.table.json (table; CC-BY-4.0)
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/go-format.md.