Overview
Package printer implements printing of AST nodes.
func Fprint
// Parse source file and extract the AST without comments for
// this function, with position information referring to the
// file set fset.
funcAST, fset := parseFunc("example_test.go", "ExampleFprint")
// The file set is provided to the printer so that it knows
// about the original source formatting and can add additional
// line breaks where they were present in the source.
var buf bytes.Buffer
// Remove braces {} enclosing the function body, unindent,
// and trim leading and trailing white space.
s := buf.String()
s = s[1 : len(s)-1]
s = strings.TrimSpace(strings.Replace(s, "\n\t", "\n", -1))
// Print the cleaned-up body text to stdout.
fmt.Println(s)
// funcAST, fset := parseFunc("example_test.go", "ExampleFprint")
//
// var buf bytes.Buffer
// printer.Fprint(&buf, fset, funcAST.Body)
//
// s := buf.String()
// s = s[1 : len(s)-1]
// s = strings.TrimSpace(strings.Replace(s, "\n\t", "\n", -1))
//
// fmt.Println(s)
A CommentedNode bundles an AST node and corresponding comments. It may be
provided as argument to any of the Fprint functions.
type
¶
- type Config struct {
- Mode // default: 0
- Tabwidth int // default: 8
- Indent // default: 0 (all code is indented at least by this much)
- }
A Config node controls the output of Fprint.
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.
A Mode value is a set of flags (or 0). They control printing.