Go printer: pretty-print Go AST nodes to text
Answer: Package printer writes Go AST nodes as text. Fprint uses default settings; Config.Fprint lets you set Mode, Tabwidth, and Indent.
Fprint
The package's job is to print AST nodes.
Fprint pretty-prints an AST node to output. It calls Config.Fprint with default settings.
gofmt indents with tabs and aligns with spaces. For output that matches gofmt, use format.Node from package go/format.
The file set is provided so the printer knows about the original source formatting and can add also additional line breaks where they were present in the source.
Config and Mode
A Config node controls the output of Fprint. Mode defaults to 0, Tabwidth defaults to 8, and Indent defaults to 0, meaning all code is indented at least by this much.
Config.Fprint pretty-prints an AST node to output for a given configuration. Position information is interpreted relative to the file set fset.
The node type must be a File, a CommentedNode, a list of Decl or Stmt, or assignment-compatible to Expr, Decl, Spec, or Stmt.
A CommentedNode bundles an AST node and corresponding comments. It may be provided as an argument to any of the Fprint functions. Node may be a File, or an Expr, Decl, Spec, or Stmt.
A Mode value is a set of flags, or 0, that control printing. RawFormat skips the tabwriter and, when set, causes UseSpaces to be ignored.
TabIndent means use tabs for indentation independent of UseSpaces. UseSpaces requests spaces rather than tabs for alignment.
SourcePos writes //line directives so readers can recover the original positions.
See also
Claims
- id: C1 claim: The package prints AST nodes. source: S2 evidence: "Package printer implements printing of AST nodes."
- id: C2 claim: Fprint pretty-prints using Config.Fprint defaults. source: S1 evidence: "pretty-prints\" an AST node to output"
- id: C3 claim: gofmt indents with tabs and aligns with spaces. source: S1 evidence: "tabs for indentation but spaces for alignment"
- id: C4 claim: Config defaults are Mode 0, Tabwidth 8, and Indent 0. source: S1 evidence: "Tabwidth int // default: 8"
- id: C5 claim: SourcePos emits //line directives for original positions. source: S1 evidence: "emit //line directives to preserve original source positions"
Sources
- [S1] printer package - go/printer. The Go Authors. https://pkg.go.dev/go/printer . License: BSD-3-Clause. Accessed 2026-11-27.
- [S2] printer.go (source). The Go Authors. https://go.dev/src/go/printer/printer.go . License: BSD-3-Clause. Accessed 2026-11-27.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: The Go Authors, printer package - go/printer; The Go Authors, printer.go (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, printer package - go/printer; The Go Authors, printer.go (source). BSD-3-Clause (https://go.dev/LICENSE). Copyright The Go Authors.
Cite this
iinventedlight. 2026. "Go printer: pretty-print Go AST nodes to text." iinventedlight Library, iil:note:r6c7exp2, version of 2026-11-27. https://iinventedlight.com/library/notes/go-printer/. License: CC-BY-4.0.
@misc{iil_note_r6c7exp2,
author = {{iinventedlight}},
title = {{Go printer: pretty-print Go AST nodes to text}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-27},
url = {https://iinventedlight.com/library/notes/go-printer/},
note = {Permanent ID iil:note:r6c7exp2. 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-printer.schema.json (schema; CC-BY-4.0)
- go-printer.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-printer.md.