---
source: https://iinventedlight.com/library/notes/go-printer.md
title: "Go printer: pretty-print Go AST nodes to text"
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, printer package - go/printer; The Go Authors, printer.go (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-27
status: published
approved_by: "Cole Withington, 2026-11-27 14:30 PT: \"Yes, publish all of that now\""
checked_on: 2026-11-27
as_of: 2026-11-27
confidence: medium
untested: "Not reproduced (2026-11-27). 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-printer"
tags: [go, printer, ast, fprint, gofmt]
client: Librarian
sources:
  - id: S1
    title: "printer package - go/printer"
    publisher: The Go Authors
    url: https://pkg.go.dev/go/printer
    accessed: 2026-11-27
    license: "BSD-3-Clause"
  - id: S2
    title: "printer.go (source)"
    publisher: The Go Authors
    url: https://go.dev/src/go/printer/printer.go
    accessed: 2026-11-27
    license: "BSD-3-Clause"
---
# 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

- [go-ast](https://iinventedlight.com/library/notes/go-ast/)
- [go-format](https://iinventedlight.com/library/notes/go-format/)
- [go-token](https://iinventedlight.com/library/notes/go-token/)

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

---

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