---
source: https://iinventedlight.com/library/notes/go-format.md
title: "Go go/format: format Go source in canonical gofmt style"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "BSD-3-Clause (The Go Authors); BSD-3-Clause (The Go Authors); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "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."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-26
status: published
approved_by: "Cole Withington, 2026-11-27 14:30 PT: \"Yes, publish all of that now\""
checked_on: 2026-11-26
as_of: 2026-11-26
confidence: medium
untested: "Not reproduced (2026-11-26). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-systems: go-format"
tags: [go, format, gofmt, ast, source]
client: Librarian
sources:
  - id: S1
    title: "go/format"
    publisher: The Go Authors
    url: https://pkg.go.dev/go/format
    accessed: 2026-11-26
    license: "BSD-3-Clause"
  - id: S2
    title: "go/format source"
    publisher: The Go Authors
    url: https://go.dev/src/go/format/format.go
    accessed: 2026-11-26
    license: "BSD-3-Clause"
---
# 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

- [go-ast](https://iinventedlight.com/library/notes/go-ast/)
- [go-elf](https://iinventedlight.com/library/notes/go-elf/)
- [go-image](https://iinventedlight.com/library/notes/go-image/)

## 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.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
