Go 语言

Go 语言教程 Go 语言环境安装 Go 语言结构 Go 语言基础语法 Go 语言数据类型 Go 语言变量 Go 语言常量 Go 语言运算符 Go 语言条件语句 Go 语言 if 语句 Go 语言 if...else 语句 Go 语言 if 语句嵌套 Go 语言 switch 语句 Go 语言 select 语句 Go 语言循环语句 Go 语言 for 循环 Go 语言循环嵌套 Go 语言 break 语句 Go 语言 continue 语句 Go 语言 goto 语句 Go 语言函数 Go 语言函数值传递值 Go 语言函数引用传递值 Go 语言函数作为值 Go 语言函数闭包 Go 语言函数方法 Go 语言变量作用域 Go 语言数组 Go 语言多维数组 Go 语言向函数传递数组 Go 语言指针 Go 语言指针数组 Go 语言指向指针的指针 Go 语言指针作为函数参数 Go 语言结构体 Go 语言切片(Slice) Go 语言范围(Range) Go 语言Map(集合) Go 语言递归函数 Go 语言类型转换 Go 语言接口 Go 错误处理 Go 语言开发工具Go 语言标准库

Go 语言标准库


package printer

import "go/printer"

Package printer implements printing of AST nodes..

Go语言标准库 >>


  • func Fprint(output io.Writer, fset *token.FileSet, node interface{}) error
  • type CommentedNode
  • type Config
  • type Mode
  • func Fprint

    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.

    // 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")
    
    // Print the function body into buffer buf.
    // 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
    printer.Fprint(&buf, fset, funcAST.Body)
    
    // 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)
    
    

    Output:

    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)
    

    type CommentedNode

    type CommentedNode struct {
        Node     interface{} // *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 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 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

    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 comments to preserve original source positions
    )