printer

package
v1.7.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 17, 2026 License: Apache-2.0 Imports: 13 Imported by: 2 modules, 1 package

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package printer implements printing of AST nodes.

Index

Constants

View Source
const (
	DbgFlagAll = 1
)

Variables

This section is empty.

Functions

func Fprint

func Fprint(output io.Writer, fset *token.FileSet, node any) error

Fprint "pretty-prints" an AST node to output. It calls Config.Fprint with default settings. Note that gofmt uses tabs for indentation but spaces for alignment; use format.Node (package go/format) for output that matches gofmt.

func SetDebug

func SetDebug(flags int)

Types

type CommentedNode

type CommentedNode struct {
	Node     any // *ast.File, or ast.Expr, ast.Decl, ast.Spec, or ast.Stmt
	Comments []*ast.CommentGroup
}

A CommentedNode bundles an AST node and corresponding comments. It may be provided as argument to any of the Fprint functions.

type CommentedNodes added in v1.6.0

type CommentedNodes struct {
	Node           any                            // *ast.File, or ast.Expr, ast.Decl, ast.Spec, or ast.Stmt
	CommentedStmts map[ast.Stmt]*ast.CommentGroup // statements with their leading comments
}

CommentedNodes holds an AST node and a map of statements with their leading comments. This allows attaching diagnostic comments to specific statements during code generation.

type Config

type Config struct {
	Mode     Mode // default: 0
	Tabwidth int  // default: 8
	Indent   int  // default: 0 (all code is indented at least by this much)
}

A Config node controls the output of Fprint.

func (*Config) Fprint

func (cfg *Config) Fprint(output io.Writer, fset *token.FileSet, node any) error

Fprint "pretty-prints" an AST node to output for a given configuration cfg. Position information is interpreted relative to the file set fset. The node type must be *ast.File, *CommentedNode, *CommentedNodes, []ast.Decl, []ast.Stmt, or assignment-compatible to ast.Expr, ast.Decl, ast.Spec, or ast.Stmt.

type Mode

type Mode uint

A Mode value is a set of flags (or 0). They control printing.

const (
	RawFormat Mode = 1 << iota // do not use a tabwriter; if set, UseSpaces is ignored
	TabIndent                  // use tabs for indentation independent of UseSpaces
	UseSpaces                  // use spaces instead of tabs for alignment
	SourcePos                  // emit //line directives to preserve original source positions
)

type NewlineStmt

type NewlineStmt struct {
	ast.EmptyStmt
}

NewlineStmt represents a statement that formats as a newline

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL