version: 1.10

package printer

import "go/printer"

Overview

Package printer implements printing of AST nodes.

Index

Examples

Package files

nodes.go printer.go

func Fprint

  1. func Fprint(output io.Writer, fset *token.FileSet, node interface{}) 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.

Example:

  1. // Parse source file and extract the AST without comments for
  2. // this function, with position information referring to the
  3. // file set fset.
  4. funcAST, fset := parseFunc("example_test.go", "ExampleFprint")
  5. // Print the function body into buffer buf.
  6. // The file set is provided to the printer so that it knows
  7. // about the original source formatting and can add additional
  8. // line breaks where they were present in the source.
  9. var buf bytes.Buffer
  10. printer.Fprint(&buf, fset, funcAST.Body)
  11. // Remove braces {} enclosing the function body, unindent,
  12. // and trim leading and trailing white space.
  13. s := buf.String()
  14. s = s[1 : len(s)-1]
  15. s = strings.TrimSpace(strings.Replace(s, "\n\t", "\n", -1))
  16. // Print the cleaned-up body text to stdout.
  17. fmt.Println(s)
  18. // output:
  19. // funcAST, fset := parseFunc("example_test.go", "ExampleFprint")
  20. //
  21. // var buf bytes.Buffer
  22. // printer.Fprint(&buf, fset, funcAST.Body)
  23. //
  24. // s := buf.String()
  25. // s = s[1 : len(s)-1]
  26. // s = strings.TrimSpace(strings.Replace(s, "\n\t", "\n", -1))
  27. //
  28. // fmt.Println(s)

type CommentedNode

  1. type CommentedNode struct {
  2. Node interface{} // *ast.File, or ast.Expr, ast.Decl, ast.Spec, or ast.Stmt
  3. Comments []*ast.CommentGroup
  4. }

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

type Config

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

A Config node controls the output of Fprint.

func (*Config) Fprint

  1. func (cfg *Config) Fprint(output io.Writer, fset *token.FileSet, node interface{}) 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, []ast.Decl, []ast.Stmt, or assignment-compatible to ast.Expr, ast.Decl, ast.Spec, or ast.Stmt.

type Mode

  1. type Mode uint

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

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