← All notes

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

Sources

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

Permanent ID: iil:note:hxg010z9

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.},
}

Markdown

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.

All artifact files (JSON index)

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.