GO语言"go/ast"包中"Print"函数的用法及代码示例。
用法:
func Print(fset *token.FileSet, x any) error
Print 将 x 打印到标准输出,跳过 nil 字段。 Print(fset, x) 与 Fprint(os.Stdout, fset, x, NotNilFilter) 相同。
例子:
此示例显示了 AST 在打印以进行调试时的样子。
package main
import (
"go/ast"
"go/parser"
"go/token"
)
func main() {
// src is the input for which we want to print the AST.
src := `
package main
func main() {
println("Hello, World!")
}
`
// Create the AST by parsing src.
fset := token.NewFileSet() // positions are relative to fset
f, err := parser.ParseFile(fset, "", src, 0)
if err != nil {
panic(err)
}
// Print the AST.
ast.Print(fset, f)
}
输出:
0 *ast.File { 1 . Package: 2:1 2 . Name: *ast.Ident { 3 . . NamePos: 2:9 4 . . Name: "main" 5 . } 6 . Decls: []ast.Decl (len = 1) { 7 . . 0: *ast.FuncDecl { 8 . . . Name: *ast.Ident { 9 . . . . NamePos: 3:6 10 . . . . Name: "main" 11 . . . . Obj: *ast.Object { 12 . . . . . Kind: func 13 . . . . . Name: "main" 14 . . . . . Decl: *(obj @ 7) 15 . . . . } 16 . . . } 17 . . . Type: *ast.FuncType { 18 . . . . Func: 3:1 19 . . . . Params: *ast.FieldList { 20 . . . . . Opening: 3:10 21 . . . . . Closing: 3:11 22 . . . . } 23 . . . } 24 . . . Body: *ast.BlockStmt { 25 . . . . Lbrace: 3:13 26 . . . . List: []ast.Stmt (len = 1) { 27 . . . . . 0: *ast.ExprStmt { 28 . . . . . . X: *ast.CallExpr { 29 . . . . . . . Fun: *ast.Ident { 30 . . . . . . . . NamePos: 4:2 31 . . . . . . . . Name: "println" 32 . . . . . . . } 33 . . . . . . . Lparen: 4:9 34 . . . . . . . Args: []ast.Expr (len = 1) { 35 . . . . . . . . 0: *ast.BasicLit { 36 . . . . . . . . . ValuePos: 4:10 37 . . . . . . . . . Kind: STRING 38 . . . . . . . . . Value: "\"Hello, World!\"" 39 . . . . . . . . } 40 . . . . . . . } 41 . . . . . . . Ellipsis: - 42 . . . . . . . Rparen: 4:25 43 . . . . . . } 44 . . . . . } 45 . . . . } 46 . . . . Rbrace: 5:1 47 . . . } 48 . . } 49 . } 50 . Scope: *ast.Scope { 51 . . Objects: map[string]*ast.Object (len = 1) { 52 . . . "main": *(obj @ 11) 53 . . } 54 . } 55 . Unresolved: []*ast.Ident (len = 1) { 56 . . 0: *(obj @ 29) 57 . } 58 }
相关用法
- GO Print用法及代码示例
- GO Printf用法及代码示例
- GO Println用法及代码示例
- GO PutUvarint用法及代码示例
- GO ParseAddress用法及代码示例
- GO PlainAuth用法及代码示例
- GO ParseUint用法及代码示例
- GO ParseIP用法及代码示例
- GO Pow10用法及代码示例
- GO ParseMediaType用法及代码示例
- GO ParseInt用法及代码示例
- GO ParseCIDR用法及代码示例
- GO PutVarint用法及代码示例
- GO Perm用法及代码示例
- GO Pipe用法及代码示例
- GO ParseInLocation用法及代码示例
- GO PathUnescape用法及代码示例
- GO ParseDuration用法及代码示例
- GO ParseFile用法及代码示例
- GO Parse用法及代码示例
注:本文由纯净天空筛选整理自golang.google.cn大神的英文原创作品 Print。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。