Package printer

import "go/printer"
Overview
Index
Examples

Overview ▾

打包打印机实现AST节点的打印.

func Fprint

func Fprint(output io.Writer, fset *token.FileSet, node interface{}) error

Fprint"漂亮地打印"要输出的AST节点. 它使用默认设置调用Config.Fprint. 请注意,gofmt使用制表符进行缩进,而使用空格进行对齐; 对与gofmt匹配的输出使用format.Node(软件包go / format).

Example

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.ReplaceAll(s, "\n\t", "\n"))

fmt.Println(s)

type CommentedNode

CommentedNode捆绑AST节点和相应的注释. 它可以作为任何Fprint函数的参数提供.

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

type Config

Config节点控制Fprint的输出.

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

func (*Config) Fprint

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

Fprint"漂亮地打印" AST节点以输出给定配置cfg. 相对于文件集fset解释位置信息. 节点类型必须为* ast.File,* CommentedNode,[] ast.Decl,[] ast.Stmt,或与ast.Expr,ast.Decl,ast.Spec或ast.Stmt分配兼容.

type Mode

模式值是一组标志(或0). 他们控制打印.

type Mode uint
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
)

by  ICOPY.SITE