Package ast

import "go/ast"
Overview
Index
Examples

Overview ▾

包ast声明用于表示Go包语法树的类型.

Index ▾

func FileExports(src *File) bool
func FilterDecl(decl Decl, f Filter) bool
func FilterFile(src *File, f Filter) bool
func FilterPackage(pkg *Package, f Filter) bool
func Fprint(w io.Writer, fset *token.FileSet, x interface{}, f FieldFilter) error
func Inspect(node Node, f func(Node) bool)
func IsExported(name string) bool
func NotNilFilter(_ string, v reflect.Value) bool
func PackageExports(pkg *Package) bool
func Print(fset *token.FileSet, x interface{}) error
func SortImports(fset *token.FileSet, f *File)
func Walk(v Visitor, node Node)
type ArrayType
    func (x *ArrayType) End() token.Pos
    func (x *ArrayType) Pos() token.Pos
type AssignStmt
    func (s *AssignStmt) End() token.Pos
    func (s *AssignStmt) Pos() token.Pos
type BadDecl
    func (d *BadDecl) End() token.Pos
    func (d *BadDecl) Pos() token.Pos
type BadExpr
    func (x *BadExpr) End() token.Pos
    func (x *BadExpr) Pos() token.Pos
type BadStmt
    func (s *BadStmt) End() token.Pos
    func (s *BadStmt) Pos() token.Pos
type BasicLit
    func (x *BasicLit) End() token.Pos
    func (x *BasicLit) Pos() token.Pos
type BinaryExpr
    func (x *BinaryExpr) End() token.Pos
    func (x *BinaryExpr) Pos() token.Pos
type BlockStmt
    func (s *BlockStmt) End() token.Pos
    func (s *BlockStmt) Pos() token.Pos
type BranchStmt
    func (s *BranchStmt) End() token.Pos
    func (s *BranchStmt) Pos() token.Pos
type CallExpr
    func (x *CallExpr) End() token.Pos
    func (x *CallExpr) Pos() token.Pos
type CaseClause
    func (s *CaseClause) End() token.Pos
    func (s *CaseClause) Pos() token.Pos
type ChanDir
type ChanType
    func (x *ChanType) End() token.Pos
    func (x *ChanType) Pos() token.Pos
type CommClause
    func (s *CommClause) End() token.Pos
    func (s *CommClause) Pos() token.Pos
type Comment
    func (c *Comment) End() token.Pos
    func (c *Comment) Pos() token.Pos
type CommentGroup
    func (g *CommentGroup) End() token.Pos
    func (g *CommentGroup) Pos() token.Pos
    func (g *CommentGroup) Text() string
type CommentMap
    func NewCommentMap(fset *token.FileSet, node Node, comments []*CommentGroup) CommentMap
    func (cmap CommentMap) Comments() []*CommentGroup
    func (cmap CommentMap) Filter(node Node) CommentMap
    func (cmap CommentMap) String() string
    func (cmap CommentMap) Update(old, new Node) Node
type CompositeLit
    func (x *CompositeLit) End() token.Pos
    func (x *CompositeLit) Pos() token.Pos
type Decl
type DeclStmt
    func (s *DeclStmt) End() token.Pos
    func (s *DeclStmt) Pos() token.Pos
type DeferStmt
    func (s *DeferStmt) End() token.Pos
    func (s *DeferStmt) Pos() token.Pos
type Ellipsis
    func (x *Ellipsis) End() token.Pos
    func (x *Ellipsis) Pos() token.Pos
type EmptyStmt
    func (s *EmptyStmt) End() token.Pos
    func (s *EmptyStmt) Pos() token.Pos
type Expr
type ExprStmt
    func (s *ExprStmt) End() token.Pos
    func (s *ExprStmt) Pos() token.Pos
type Field
    func (f *Field) End() token.Pos
    func (f *Field) Pos() token.Pos
type FieldFilter
type FieldList
    func (f *FieldList) End() token.Pos
    func (f *FieldList) NumFields() int
    func (f *FieldList) Pos() token.Pos
type File
    func MergePackageFiles(pkg *Package, mode MergeMode) *File
    func (f *File) End() token.Pos
    func (f *File) Pos() token.Pos
type Filter
type ForStmt
    func (s *ForStmt) End() token.Pos
    func (s *ForStmt) Pos() token.Pos
type FuncDecl
    func (d *FuncDecl) End() token.Pos
    func (d *FuncDecl) Pos() token.Pos
type FuncLit
    func (x *FuncLit) End() token.Pos
    func (x *FuncLit) Pos() token.Pos
type FuncType
    func (x *FuncType) End() token.Pos
    func (x *FuncType) Pos() token.Pos
type GenDecl
    func (d *GenDecl) End() token.Pos
    func (d *GenDecl) Pos() token.Pos
type GoStmt
    func (s *GoStmt) End() token.Pos
    func (s *GoStmt) Pos() token.Pos
type Ident
    func NewIdent(name string) *Ident
    func (x *Ident) End() token.Pos
    func (id *Ident) IsExported() bool
    func (x *Ident) Pos() token.Pos
    func (id *Ident) String() string
type IfStmt
    func (s *IfStmt) End() token.Pos
    func (s *IfStmt) Pos() token.Pos
type ImportSpec
    func (s *ImportSpec) End() token.Pos
    func (s *ImportSpec) Pos() token.Pos
type Importer
type IncDecStmt
    func (s *IncDecStmt) End() token.Pos
    func (s *IncDecStmt) Pos() token.Pos
type IndexExpr
    func (x *IndexExpr) End() token.Pos
    func (x *IndexExpr) Pos() token.Pos
type InterfaceType
    func (x *InterfaceType) End() token.Pos
    func (x *InterfaceType) Pos() token.Pos
type KeyValueExpr
    func (x *KeyValueExpr) End() token.Pos
    func (x *KeyValueExpr) Pos() token.Pos
type LabeledStmt
    func (s *LabeledStmt) End() token.Pos
    func (s *LabeledStmt) Pos() token.Pos
type MapType
    func (x *MapType) End() token.Pos
    func (x *MapType) Pos() token.Pos
type MergeMode
type Node
type ObjKind
    func (kind ObjKind) String() string
type Object
    func NewObj(kind ObjKind, name string) *Object
    func (obj *Object) Pos() token.Pos
type Package
    func NewPackage(fset *token.FileSet, files map[string]*File, importer Importer, universe *Scope) (*Package, error)
    func (p *Package) End() token.Pos
    func (p *Package) Pos() token.Pos
type ParenExpr
    func (x *ParenExpr) End() token.Pos
    func (x *ParenExpr) Pos() token.Pos
type RangeStmt
    func (s *RangeStmt) End() token.Pos
    func (s *RangeStmt) Pos() token.Pos
type ReturnStmt
    func (s *ReturnStmt) End() token.Pos
    func (s *ReturnStmt) Pos() token.Pos
type Scope
    func NewScope(outer *Scope) *Scope
    func (s *Scope) Insert(obj *Object) (alt *Object)
    func (s *Scope) Lookup(name string) *Object
    func (s *Scope) String() string
type SelectStmt
    func (s *SelectStmt) End() token.Pos
    func (s *SelectStmt) Pos() token.Pos
type SelectorExpr
    func (x *SelectorExpr) End() token.Pos
    func (x *SelectorExpr) Pos() token.Pos
type SendStmt
    func (s *SendStmt) End() token.Pos
    func (s *SendStmt) Pos() token.Pos
type SliceExpr
    func (x *SliceExpr) End() token.Pos
    func (x *SliceExpr) Pos() token.Pos
type Spec
type StarExpr
    func (x *StarExpr) End() token.Pos
    func (x *StarExpr) Pos() token.Pos
type Stmt
type StructType
    func (x *StructType) End() token.Pos
    func (x *StructType) Pos() token.Pos
type SwitchStmt
    func (s *SwitchStmt) End() token.Pos
    func (s *SwitchStmt) Pos() token.Pos
type TypeAssertExpr
    func (x *TypeAssertExpr) End() token.Pos
    func (x *TypeAssertExpr) Pos() token.Pos
type TypeSpec
    func (s *TypeSpec) End() token.Pos
    func (s *TypeSpec) Pos() token.Pos
type TypeSwitchStmt
    func (s *TypeSwitchStmt) End() token.Pos
    func (s *TypeSwitchStmt) Pos() token.Pos
type UnaryExpr
    func (x *UnaryExpr) End() token.Pos
    func (x *UnaryExpr) Pos() token.Pos
type ValueSpec
    func (s *ValueSpec) End() token.Pos
    func (s *ValueSpec) Pos() token.Pos
type Visitor

Examples

CommentMap
Inspect
Print

Package files

ast.go commentmap.go filter.go import.go print.go resolve.go scope.go walk.go

func FileExports

func FileExports(src *File) bool

FileExports在适当的位置修剪Go源文件的AST,以便仅保留导出的节点:所有未导出的顶级标识符及其相关信息(例如类型,初始值或函数体)都将被删除. 删除非导出字段和导出类型的方法. File.Comments列表未更改.

FileExports报告是否存在导出的声明.

func FilterDecl

func FilterDecl(decl Decl, f Filter) bool

FilterDecl通过删除所有不通过过滤器f的名称(包括结构域和接口方法名称,但不从参数列表中删除)来修剪Go声明的AST.

FilterDecl报告过滤后是否还有声明的名称.

func FilterFile

func FilterFile(src *File, f Filter) bool

FilterFile通过从不通过过滤器f的顶级声明(包括结构域和接口方法名称,但不从参数列表中)删除所有名称来修剪Go文件的AST. 如果声明之后为空,则将该声明从AST中删除. 导入声明始终被删除. File.Comments列表未更改.

FilterFile报告过滤后是否还有任何顶级声明.

func FilterPackage

func FilterPackage(pkg *Package, f Filter) bool

FilterPackage通过从不通过过滤器f的顶级声明中删除所有名称(包括结构域和接口方法名称,但不从参数列表中)来修剪Go程序包的AST. 如果声明之后为空,则将该声明从AST中删除. pkg.Files列表不会更改,因此文件名和顶级软件包注释不会丢失.

FilterPackage reports whether there are any top-level declarations left after filtering.

func Fprint

func Fprint(w io.Writer, fset *token.FileSet, x interface{}, f FieldFilter) error

Fprint将(子)树从AST节点x开始打印到w. 如果fset!= nil,则相对于该文件集解释位置信息. 否则,位置将打印为整数值(文件集特定的偏移量).

可以提供一个非nil的FieldFilter f来控制输出:打印f(fieldname,fieldvalue)为true的结构字段; 从输出中过滤掉所有其他过滤器. 未导出的结构字段从不打印.

func Inspect

func Inspect(node Node, f func(Node) bool)

检查以深度优先顺序遍历AST:首先调用f(node); 节点不能为零. 如果f返回true,Inspect将为node的每个非nil子代递归调用f,然后调用f(nil).

Example

本示例演示了如何检查Go程序的AST.

src.go:2:9:	p
src.go:3:7:	c
src.go:3:11:	1.0
src.go:4:5:	X
src.go:4:9:	f
src.go:4:11:	3.14
src.go:4:17:	2
src.go:4:21:	c

func IsExported

func IsExported(name string) bool

IsExported报告名称是否以大写字母开头.

func NotNilFilter

func NotNilFilter(_ string, v reflect.Value) bool

对于非nil的字段值,NotNilFilter返回true; 否则返回false.

func PackageExports

func PackageExports(pkg *Package) bool

PackageExports在适当位置修剪Go包的AST,以便仅保留导出的节点. pkg.Files列表不会更改,因此文件名和顶级软件包注释不会丢失.

PackageExports报告是否有导出的声明; 否则返回false.

func Print

func Print(fset *token.FileSet, x interface{}) error

Print将x打印到标准输出,跳过nil字段. Print(fset,x)与Fprint(os.Stdout,fset,x,NotNilFilter)相同.

Example

本示例说明了为调试而打印时AST的外观.

     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  }

func SortImports

func SortImports(fset *token.FileSet, f *File)

SortImports在f中的导入块中对连续的导入行进行排序. 如果可能,它还会删除重复的导入而不会丢失数据.

func Walk

func Walk(v Visitor, node Node)

漫游以深度优先的顺序遍历AST:它通过调用v.Visit(node)开始; 节点不能为零. 如果v.Visit(node)返回的访问者w不为nil,则对节点的每个非nil子节点与访问者w递归地调用Walk,然后调用w.Visit(nil).

type ArrayType

ArrayType节点表示数组或切片类型.

type ArrayType struct {
    Lbrack token.Pos // position of "["
    Len    Expr      // Ellipsis node for [...]T array types, nil for slice types
    Elt    Expr      // element type
}

func (*ArrayType) End

func (x *ArrayType) End() token.Pos

func (*ArrayType) Pos

func (x *ArrayType) Pos() token.Pos

type AssignStmt

AssignStmt节点表示分配或简短变量声明.

type AssignStmt struct {
    Lhs    []Expr
    TokPos token.Pos   // position of Tok
    Tok    token.Token // assignment token, DEFINE
    Rhs    []Expr
}

func (*AssignStmt) End

func (s *AssignStmt) End() token.Pos

func (*AssignStmt) Pos

func (s *AssignStmt) Pos() token.Pos

type BadDecl

BadDecl节点是包含语法错误的声明的占位符,无法为其创建正确的声明节点.

type BadDecl struct {
    From, To token.Pos // position range of bad declaration
}

func (*BadDecl) End

func (d *BadDecl) End() token.Pos

func (*BadDecl) Pos

func (d *BadDecl) Pos() token.Pos

type BadExpr

A BadExpr node is a placeholder for expressions containing syntax errors for which no correct expression nodes can be created.

type BadExpr struct {
    From, To token.Pos // position range of bad expression
}

func (*BadExpr) End

func (x *BadExpr) End() token.Pos

func (*BadExpr) Pos

func (x *BadExpr) Pos() token.Pos

type BadStmt

BadStmt节点是包含语法错误的语句的占位符,无法为其创建正确的语句节点.

type BadStmt struct {
    From, To token.Pos // position range of bad statement
}

func (*BadStmt) End

func (s *BadStmt) End() token.Pos

func (*BadStmt) Pos

func (s *BadStmt) Pos() token.Pos

type BasicLit

BasicLit节点表示基本类型的文字.

type BasicLit struct {
    ValuePos token.Pos   // literal position
    Kind     token.Token // token.INT, token.FLOAT, token.IMAG, token.CHAR, or token.STRING
    Value    string      // literal string; e.g. 42, 0x7f, 3.14, 1e-9, 2.4i, 'a', '\x7f', "foo" or `\m\n\o`
}

func (*BasicLit) End

func (x *BasicLit) End() token.Pos

func (*BasicLit) Pos

func (x *BasicLit) Pos() token.Pos

type BinaryExpr

BinaryExpr节点表示一个二进制表达式.

type BinaryExpr struct {
    X     Expr        // left operand
    OpPos token.Pos   // position of Op
    Op    token.Token // operator
    Y     Expr        // right operand
}

func (*BinaryExpr) End

func (x *BinaryExpr) End() token.Pos

func (*BinaryExpr) Pos

func (x *BinaryExpr) Pos() token.Pos

type BlockStmt

BlockStmt节点表示支撑语句列表.

type BlockStmt struct {
    Lbrace token.Pos // position of "{"
    List   []Stmt
    Rbrace token.Pos // position of "}"
}

func (*BlockStmt) End

func (s *BlockStmt) End() token.Pos

func (*BlockStmt) Pos

func (s *BlockStmt) Pos() token.Pos

type BranchStmt

BranchStmt节点表示break,continue,goto或fallthrough语句.

type BranchStmt struct {
    TokPos token.Pos   // position of Tok
    Tok    token.Token // keyword token (BREAK, CONTINUE, GOTO, FALLTHROUGH)
    Label  *Ident      // label name; or nil
}

func (*BranchStmt) End

func (s *BranchStmt) End() token.Pos

func (*BranchStmt) Pos

func (s *BranchStmt) Pos() token.Pos

type CallExpr

CallExpr节点表示一个表达式,后跟一个参数列表.

type CallExpr struct {
    Fun      Expr      // function expression
    Lparen   token.Pos // position of "("
    Args     []Expr    // function arguments; or nil
    Ellipsis token.Pos // position of "..." (token.NoPos if there is no "...")
    Rparen   token.Pos // position of ")"
}

func (*CallExpr) End

func (x *CallExpr) End() token.Pos

func (*CallExpr) Pos

func (x *CallExpr) Pos() token.Pos

type CaseClause

CaseClause表示表达式或类型切换语句的大小写.

type CaseClause struct {
    Case  token.Pos // position of "case" or "default" keyword
    List  []Expr    // list of expressions or types; nil means default case
    Colon token.Pos // position of ":"
    Body  []Stmt    // statement list; or nil
}

func (*CaseClause) End

func (s *CaseClause) End() token.Pos

func (*CaseClause) Pos

func (s *CaseClause) Pos() token.Pos

type ChanDir

通道类型的方向由包含以下一个或两个常量的位掩码指示.

type ChanDir int
const (
    SEND ChanDir = 1 << iota
    RECV
)

type ChanType

ChanType节点表示通道类型.

type ChanType struct {
    Begin token.Pos // position of "chan" keyword or "<-" (whichever comes first)
    Arrow token.Pos // position of "<-" (token.NoPos if there is no "<-"); added in Go 1.1
    Dir   ChanDir   // channel direction
    Value Expr      // value type
}

func (*ChanType) End

func (x *ChanType) End() token.Pos

func (*ChanType) Pos

func (x *ChanType) Pos() token.Pos

type CommClause

CommClause节点表示选择语句的情况.

type CommClause struct {
    Case  token.Pos // position of "case" or "default" keyword
    Comm  Stmt      // send or receive statement; nil means default case
    Colon token.Pos // position of ":"
    Body  []Stmt    // statement list; or nil
}

func (*CommClause) End

func (s *CommClause) End() token.Pos

func (*CommClause) Pos

func (s *CommClause) Pos() token.Pos

type Comment

Comment节点表示单个//样式或/ *样式的注释.

type Comment struct {
    Slash token.Pos // position of "/" starting the comment
    Text  string    // comment text (excluding '\n' for //-style comments)
}

func (*Comment) End

func (c *Comment) End() token.Pos

func (*Comment) Pos

func (c *Comment) Pos() token.Pos

type CommentGroup

CommentGroup表示注释序列,其中没有其他标记,并且之间没有空行.

type CommentGroup struct {
    List []*Comment // len(List) > 0
}

func (*CommentGroup) End

func (g *CommentGroup) End() token.Pos

func (*CommentGroup) Pos

func (g *CommentGroup) Pos() token.Pos

func (*CommentGroup) Text

func (g *CommentGroup) Text() string

文字返回评论文字. 注释标记(//,/ *和* /),行注释的第一个空格以及开头和结尾的空行都将被删除. 多个空行减少为一,并修剪行尾的空格. 除非结果为空,否则以换行符结尾.

type CommentMap 1.1

CommentMap将AST节点映射到与其关联的注释组列表. 有关关联的描述,请参见NewCommentMap.

type CommentMap map[Node][]*CommentGroup

Example

本示例说明了如何在ast程序中删除变量声明,同时使用ast.CommentMap维护正确的注释关联.

// This is the package comment.
package main

// This comment is associated with the hello constant.
const hello = "Hello, World!" // line comment 1

// This comment is associated with the main function.
func main() {
	fmt.Println(hello) // line comment 3
}

func NewCommentMap 1.1

func NewCommentMap(fset *token.FileSet, node Node, comments []*CommentGroup) CommentMap

NewCommentMap通过将注释列表的注释组与节点指定的AST的节点相关联来创建新的注释图.

如果满足以下条件,则注释组g与节点n相关联:

- g starts on the same line as n ends
- g starts on the line immediately following n, and there is
  at least one empty line after g and before the next node
- g starts before n and is not associated to the node before n
  via the previous rules

NewCommentMap尝试将注释组关联到可能的"最大"节点:例如,如果注释是在分配之后的行注释,则注释将与整个分配相关联,而不仅仅是分配中的最后一个操作数.

func (CommentMap) Comments 1.1

func (cmap CommentMap) Comments() []*CommentGroup

评论返回评论图中的评论组列表. 结果按源顺序排序.

func (CommentMap) Filter 1.1

func (cmap CommentMap) Filter(node Node) CommentMap

过滤器返回一个新的注释图,该注释图仅由cmap的那些条目组成,这些条目的相应节点在node指定的AST中存在.

func (CommentMap) String 1.1

func (cmap CommentMap) String() string

func (CommentMap) Update 1.1

func (cmap CommentMap) Update(old, new Node) Node

Update将注释映射中的旧节点替换为新节点,并返回新节点. 与旧节点关联的注释与新节点关联.

type CompositeLit

CompositeLit节点表示一个复合文字.

type CompositeLit struct {
    Type       Expr      // literal type; or nil
    Lbrace     token.Pos // position of "{"
    Elts       []Expr    // list of composite elements; or nil
    Rbrace     token.Pos // position of "}"
    Incomplete bool      // true if (source) expressions are missing in the Elts list; added in Go 1.11
}

func (*CompositeLit) End

func (x *CompositeLit) End() token.Pos

func (*CompositeLit) Pos

func (x *CompositeLit) Pos() token.Pos

type Decl

所有声明节点都实现Decl接口.

type Decl interface {
    Node
    // contains filtered or unexported methods
}

type DeclStmt

DeclStmt节点表示语句列表中的声明.

type DeclStmt struct {
    Decl Decl // *GenDecl with CONST, TYPE, or VAR token
}

func (*DeclStmt) End

func (s *DeclStmt) End() token.Pos

func (*DeclStmt) Pos

func (s *DeclStmt) Pos() token.Pos

type DeferStmt

DeferStmt节点表示defer语句.

type DeferStmt struct {
    Defer token.Pos // position of "defer" keyword
    Call  *CallExpr
}

func (*DeferStmt) End

func (s *DeferStmt) End() token.Pos

func (*DeferStmt) Pos

func (s *DeferStmt) Pos() token.Pos

type Ellipsis

省略号节点代表参数列表中的" ..."类型或数组类型中的" ..."长度.

type Ellipsis struct {
    Ellipsis token.Pos // position of "..."
    Elt      Expr      // ellipsis element type (parameter lists only); or nil
}

func (*Ellipsis) End

func (x *Ellipsis) End() token.Pos

func (*Ellipsis) Pos

func (x *Ellipsis) Pos() token.Pos

type EmptyStmt

EmptyStmt节点表示一个空语句. 空语句的"位置"是紧随其后的(显式或隐式)分号的位置.

type EmptyStmt struct {
    Semicolon token.Pos // position of following ";"
    Implicit  bool      // if set, ";" was omitted in the source; added in Go 1.5
}

func (*EmptyStmt) End

func (s *EmptyStmt) End() token.Pos

func (*EmptyStmt) Pos

func (s *EmptyStmt) Pos() token.Pos

type Expr

所有表达式节点都实现Expr接口.

type Expr interface {
    Node
    // contains filtered or unexported methods
}

type ExprStmt

ExprStmt节点表示语句列表中的(独立)表达式.

type ExprStmt struct {
    X Expr // expression
}

func (*ExprStmt) End

func (s *ExprStmt) End() token.Pos

func (*ExprStmt) Pos

func (s *ExprStmt) Pos() token.Pos

type Field

字段表示结构类型的字段声明列表,接口类型的方法列表或签名中的参数/结果声明. 对于未命名的参数(仅包含类型的参数列表)和嵌入式结构字段,Field.Names为nil. 在后一种情况下,字段名称是类型名称.

type Field struct {
    Doc     *CommentGroup // associated documentation; or nil
    Names   []*Ident      // field/method/parameter names; or nil
    Type    Expr          // field/method/parameter type
    Tag     *BasicLit     // field tag; or nil
    Comment *CommentGroup // line comments; or nil
}

func (*Field) End

func (f *Field) End() token.Pos

func (*Field) Pos

func (f *Field) Pos() token.Pos

type FieldFilter

可以将FieldFilter提供给Fprint以控制输出.

type FieldFilter func(name string, value reflect.Value) bool

type FieldList

FieldList表示字段列表,用括号或大括号括起来.

type FieldList struct {
    Opening token.Pos // position of opening parenthesis/brace, if any
    List    []*Field  // field list; or nil
    Closing token.Pos // position of closing parenthesis/brace, if any
}

func (*FieldList) End

func (f *FieldList) End() token.Pos

func (*FieldList) NumFields

func (f *FieldList) NumFields() int

NumFields返回由FieldList表示的参数或结构字段的数量.

func (*FieldList) Pos

func (f *FieldList) Pos() token.Pos

type File

File节点代表Go源文件.

The Comments list contains all comments in the source file in order of appearance, including the comments that are pointed to from other nodes via Doc and Comment fields.

为了正确地打印包含注释的源代码(使用软件包go / format和go / printer),在修改文件的语法树时必须特别注意更新注释:对于打印,注释根据标记的位置散布在标记之间. 如果语法树节点被删除或移动,则它们附近的相关注释也必须被删除(从File.Comments列表中)或被相应地移动(通过更新其位置). CommentMap可用于促进其中一些操作.

注释是否与节点相关联以及如何与节点相关联取决于操作程序对语法树的解释:除了直接与节点相关联的Doc和Comment注释外,其余注释都是"自由浮动的"(另请参见问题#18593, #20744).

type File struct {
    Doc        *CommentGroup   // associated documentation; or nil
    Package    token.Pos       // position of "package" keyword
    Name       *Ident          // package name
    Decls      []Decl          // top-level declarations; or nil
    Scope      *Scope          // package scope (this file only)
    Imports    []*ImportSpec   // imports in this file
    Unresolved []*Ident        // unresolved identifiers in this file
    Comments   []*CommentGroup // list of all comments in the source file
}

func MergePackageFiles

func MergePackageFiles(pkg *Package, mode MergeMode) *File

MergePackageFiles通过合并属于包的文件的AST来创建文件AST. 模式标志控制合并行为.

func (*File) End

func (f *File) End() token.Pos

func (*File) Pos

func (f *File) Pos() token.Pos

type Filter

type Filter func(string) bool

type ForStmt

ForStmt代表for语句.

type ForStmt struct {
    For  token.Pos // position of "for" keyword
    Init Stmt      // initialization statement; or nil
    Cond Expr      // condition; or nil
    Post Stmt      // post iteration statement; or nil
    Body *BlockStmt
}

func (*ForStmt) End

func (s *ForStmt) End() token.Pos

func (*ForStmt) Pos

func (s *ForStmt) Pos() token.Pos

type FuncDecl

FuncDecl节点表示一个函数声明.

type FuncDecl struct {
    Doc  *CommentGroup // associated documentation; or nil
    Recv *FieldList    // receiver (methods); or nil (functions)
    Name *Ident        // function/method name
    Type *FuncType     // function signature: parameters, results, and position of "func" keyword
    Body *BlockStmt    // function body; or nil for external (non-Go) function
}

func (*FuncDecl) End

func (d *FuncDecl) End() token.Pos

func (*FuncDecl) Pos

func (d *FuncDecl) Pos() token.Pos

type FuncLit

FuncLit节点表示函数文字.

type FuncLit struct {
    Type *FuncType  // function type
    Body *BlockStmt // function body
}

func (*FuncLit) End

func (x *FuncLit) End() token.Pos

func (*FuncLit) Pos

func (x *FuncLit) Pos() token.Pos

type FuncType

FuncType节点表示函数类型.

type FuncType struct {
    Func    token.Pos  // position of "func" keyword (token.NoPos if there is no "func")
    Params  *FieldList // (incoming) parameters; non-nil
    Results *FieldList // (outgoing) results; or nil
}

func (*FuncType) End

func (x *FuncType) End() token.Pos

func (*FuncType) Pos

func (x *FuncType) Pos() token.Pos

type GenDecl

GenDecl节点(通用声明节点)表示导入,常量,类型或变量声明. 有效的Lparen位置(Lparen.IsValid())表示带括号的声明.

Tok值与Specs元素类型之间的关系:

token.IMPORT  *ImportSpec
token.CONST   *ValueSpec
token.TYPE    *TypeSpec
token.VAR     *ValueSpec
type GenDecl struct {
    Doc    *CommentGroup // associated documentation; or nil
    TokPos token.Pos     // position of Tok
    Tok    token.Token   // IMPORT, CONST, TYPE, VAR
    Lparen token.Pos     // position of '(', if any
    Specs  []Spec
    Rparen token.Pos // position of ')', if any
}

func (*GenDecl) End

func (d *GenDecl) End() token.Pos

func (*GenDecl) Pos

func (d *GenDecl) Pos() token.Pos

type GoStmt

GoStmt节点表示go语句.

type GoStmt struct {
    Go   token.Pos // position of "go" keyword
    Call *CallExpr
}

func (*GoStmt) End

func (s *GoStmt) End() token.Pos

func (*GoStmt) Pos

func (s *GoStmt) Pos() token.Pos

type Ident

Ident节点代表一个标识符.

type Ident struct {
    NamePos token.Pos // identifier position
    Name    string    // identifier name
    Obj     *Object   // denoted object; or nil
}

func NewIdent

func NewIdent(name string) *Ident

NewIdent创建一个没有位置的新Ident. 对于由Go解析器以外的代码生成的AST很有用.

func (*Ident) End

func (x *Ident) End() token.Pos

func (*Ident) IsExported

func (id *Ident) IsExported() bool

IsExported报告id是否以大写字母开头.

func (*Ident) Pos

func (x *Ident) Pos() token.Pos

func (*Ident) String

func (id *Ident) String() string

type IfStmt

IfStmt节点表示一个if语句.

type IfStmt struct {
    If   token.Pos // position of "if" keyword
    Init Stmt      // initialization statement; or nil
    Cond Expr      // condition
    Body *BlockStmt
    Else Stmt // else branch; or nil
}

func (*IfStmt) End

func (s *IfStmt) End() token.Pos

func (*IfStmt) Pos

func (s *IfStmt) Pos() token.Pos

type ImportSpec

ImportSpec节点表示单个软件包导入.

type ImportSpec struct {
    Doc     *CommentGroup // associated documentation; or nil
    Name    *Ident        // local package name (including "."); or nil
    Path    *BasicLit     // import path
    Comment *CommentGroup // line comments; or nil
    EndPos  token.Pos     // end of spec (overrides Path.Pos if nonzero)
}

func (*ImportSpec) End

func (s *ImportSpec) End() token.Pos

func (*ImportSpec) Pos

func (s *ImportSpec) Pos() token.Pos

type Importer

导入程序解析导入路径以打包对象. 导入映射记录已导入的软件包,并按软件包ID(规范的导入路径)索引. 进口商必须确定规范的导入路径,并检查地图以查看其是否已存在于导入地图中. 如果是这样,则进口商可以返回地图条目. 否则,导入程序应将给定路径的包数据加载到新的* Object(pkg)中,在导入映射中记录pkg,然后返回pkg.

type Importer func(imports map[string]*Object, path string) (pkg *Object, err error)

type IncDecStmt

IncDecStmt节点表示一个递增或递减语句.

type IncDecStmt struct {
    X      Expr
    TokPos token.Pos   // position of Tok
    Tok    token.Token // INC or DEC
}

func (*IncDecStmt) End

func (s *IncDecStmt) End() token.Pos

func (*IncDecStmt) Pos

func (s *IncDecStmt) Pos() token.Pos

type IndexExpr

IndexExpr节点表示一个表达式,后跟一个索引.

type IndexExpr struct {
    X      Expr      // expression
    Lbrack token.Pos // position of "["
    Index  Expr      // index expression
    Rbrack token.Pos // position of "]"
}

func (*IndexExpr) End

func (x *IndexExpr) End() token.Pos

func (*IndexExpr) Pos

func (x *IndexExpr) Pos() token.Pos

type InterfaceType

InterfaceType节点表示接口类型.

type InterfaceType struct {
    Interface  token.Pos  // position of "interface" keyword
    Methods    *FieldList // list of methods
    Incomplete bool       // true if (source) methods are missing in the Methods list
}

func (*InterfaceType) End

func (x *InterfaceType) End() token.Pos

func (*InterfaceType) Pos

func (x *InterfaceType) Pos() token.Pos

type KeyValueExpr

KeyValueExpr节点以复合文字形式表示(键:值)对.

type KeyValueExpr struct {
    Key   Expr
    Colon token.Pos // position of ":"
    Value Expr
}

func (*KeyValueExpr) End

func (x *KeyValueExpr) End() token.Pos

func (*KeyValueExpr) Pos

func (x *KeyValueExpr) Pos() token.Pos

type LabeledStmt

LabeledStmt节点表示一个带标签的语句.

type LabeledStmt struct {
    Label *Ident
    Colon token.Pos // position of ":"
    Stmt  Stmt
}

func (*LabeledStmt) End

func (s *LabeledStmt) End() token.Pos

func (*LabeledStmt) Pos

func (s *LabeledStmt) Pos() token.Pos

type MapType

MapType节点代表地图类型.

type MapType struct {
    Map   token.Pos // position of "map" keyword
    Key   Expr
    Value Expr
}

func (*MapType) End

func (x *MapType) End() token.Pos

func (*MapType) Pos

func (x *MapType) Pos() token.Pos

type MergeMode

MergeMode标志控制MergePackageFiles的行为.

type MergeMode uint
const (
    // If set, duplicate function declarations are excluded.
    FilterFuncDuplicates MergeMode = 1 << iota
    // If set, comments that are not associated with a specific
    // AST node (as Doc or Comment) are excluded.
    FilterUnassociatedComments
    // If set, duplicate import declarations are excluded.
    FilterImportDuplicates
)

type Node

所有节点类型都实现Node接口.

type Node interface {
    Pos() token.Pos // position of first character belonging to the node
    End() token.Pos // position of first character immediately after the node
}

type ObjKind

ObjKind描述对象代表什么.

type ObjKind int

可能的对象种类列表.

const (
    Bad ObjKind = iota // for error handling
    Pkg                // package
    Con                // constant
    Typ                // type
    Var                // variable
    Fun                // function or method
    Lbl                // label
)

func (ObjKind) String

func (kind ObjKind) String() string

type Object

对象描述命名的语言实体,例如包,常量,类型,变量,函数(包括方法)或标签.

数据字段包含特定于对象的数据:

Kind    Data type         Data value
Pkg     *Scope            package scope
Con     int               iota for the respective declaration
type Object struct {
    Kind ObjKind
    Name string      // declared name
    Decl interface{} // corresponding Field, XxxSpec, FuncDecl, LabeledStmt, AssignStmt, Scope; or nil
    Data interface{} // object-specific data; or nil
    Type interface{} // placeholder for type information; may be nil
}

func NewObj

func NewObj(kind ObjKind, name string) *Object

NewObj创建给定种类和名称的新对象.

func (*Object) Pos

func (obj *Object) Pos() token.Pos

Pos计算对象名称声明的源位置. 如果无法计算结果,则结果可能是无效位置(obj.Decl可能为nil或不正确).

type Package

Package节点表示一组共同构建Go软件包的源文件.

type Package struct {
    Name    string             // package name
    Scope   *Scope             // package scope across all files
    Imports map[string]*Object // map of package id -> package object
    Files   map[string]*File   // Go source files by filename
}

func NewPackage

func NewPackage(fset *token.FileSet, files map[string]*File, importer Importer, universe *Scope) (*Package, error)

NewPackage从一组File节点创建一个新的Package节点. 它解析文件中未解析的标识符,并相应地更新每个文件的未解析列表. 如果提供了非null的importer和Universe范围,则它们将用于解析未在任何软件包文件中声明的标识符. 任何剩余的未解析的标识符都报告为未声明. 如果文件属于不同的软件包,则选择一个软件包名称,并报告具有不同软件包名称的文件,然后将其忽略. 结果是一个包节点和一个scanner.ErrorList(如果有错误).

func (*Package) End

func (p *Package) End() token.Pos

func (*Package) Pos

func (p *Package) Pos() token.Pos

type ParenExpr

ParenExpr节点表示一个带括号的表达式.

type ParenExpr struct {
    Lparen token.Pos // position of "("
    X      Expr      // parenthesized expression
    Rparen token.Pos // position of ")"
}

func (*ParenExpr) End

func (x *ParenExpr) End() token.Pos

func (*ParenExpr) Pos

func (x *ParenExpr) Pos() token.Pos

type RangeStmt

RangeStmt代表带有range子句的for语句.

type RangeStmt struct {
    For        token.Pos   // position of "for" keyword
    Key, Value Expr        // Key, Value may be nil
    TokPos     token.Pos   // position of Tok; invalid if Key == nil
    Tok        token.Token // ILLEGAL if Key == nil, ASSIGN, DEFINE
    X          Expr        // value to range over
    Body       *BlockStmt
}

func (*RangeStmt) End

func (s *RangeStmt) End() token.Pos

func (*RangeStmt) Pos

func (s *RangeStmt) Pos() token.Pos

type ReturnStmt

ReturnStmt节点代表一个return语句.

type ReturnStmt struct {
    Return  token.Pos // position of "return" keyword
    Results []Expr    // result expressions; or nil
}

func (*ReturnStmt) End

func (s *ReturnStmt) End() token.Pos

func (*ReturnStmt) Pos

func (s *ReturnStmt) Pos() token.Pos

type Scope

范围维护在该范围中声明的一组命名语言实体,以及到紧邻的(外部)范围的链接.

type Scope struct {
    Outer   *Scope
    Objects map[string]*Object
}

func NewScope

func NewScope(outer *Scope) *Scope

NewScope创建一个嵌套在外部范围内的新范围.

func (*Scope) Insert

func (s *Scope) Insert(obj *Object) (alt *Object)

插入尝试将命名对象obj插入范围s中. 如果作用域已经包含具有相同名称的对象alt,则插入将使作用域保持不变并返回alt. 否则,它将插入obj并返回nil.

func (*Scope) Lookup

func (s *Scope) Lookup(name string) *Object

如果在范围s中找到对象,查找将返回具有给定名称的对象,否则返回nil. 外部范围将被忽略.

func (*Scope) String

func (s *Scope) String() string

调试支持

type SelectStmt

SelectStmt节点表示一条select语句.

type SelectStmt struct {
    Select token.Pos  // position of "select" keyword
    Body   *BlockStmt // CommClauses only
}

func (*SelectStmt) End

func (s *SelectStmt) End() token.Pos

func (*SelectStmt) Pos

func (s *SelectStmt) Pos() token.Pos

type SelectorExpr

SelectorExpr节点表示一个表达式,后跟一个选择器.

type SelectorExpr struct {
    X   Expr   // expression
    Sel *Ident // field selector
}

func (*SelectorExpr) End

func (x *SelectorExpr) End() token.Pos

func (*SelectorExpr) Pos

func (x *SelectorExpr) Pos() token.Pos

type SendStmt

SendStmt节点代表一个send语句.

type SendStmt struct {
    Chan  Expr
    Arrow token.Pos // position of "<-"
    Value Expr
}

func (*SendStmt) End

func (s *SendStmt) End() token.Pos

func (*SendStmt) Pos

func (s *SendStmt) Pos() token.Pos

type SliceExpr

SliceExpr节点表示一个表达式,后跟切片索引.

type SliceExpr struct {
    X      Expr      // expression
    Lbrack token.Pos // position of "["
    Low    Expr      // begin of slice range; or nil
    High   Expr      // end of slice range; or nil
    Max    Expr      // maximum capacity of slice; or nil; added in Go 1.2
    Slice3 bool      // true if 3-index slice (2 colons present); added in Go 1.2
    Rbrack token.Pos // position of "]"
}

func (*SliceExpr) End

func (x *SliceExpr) End() token.Pos

func (*SliceExpr) Pos

func (x *SliceExpr) Pos() token.Pos

type Spec

Spec类型代表* ImportSpec,* ValueSpec和* TypeSpec中的任何一个.

type Spec interface {
    Node
    // contains filtered or unexported methods
}

type StarExpr

StarExpr节点表示形式为" *"表达式的表达式. 语义上可以是一元" *"表达式或指针类型.

type StarExpr struct {
    Star token.Pos // position of "*"
    X    Expr      // operand
}

func (*StarExpr) End

func (x *StarExpr) End() token.Pos

func (*StarExpr) Pos

func (x *StarExpr) Pos() token.Pos

type Stmt

所有语句节点都实现Stmt接口.

type Stmt interface {
    Node
    // contains filtered or unexported methods
}

type StructType

StructType节点表示结构类型.

type StructType struct {
    Struct     token.Pos  // position of "struct" keyword
    Fields     *FieldList // list of field declarations
    Incomplete bool       // true if (source) fields are missing in the Fields list
}

func (*StructType) End

func (x *StructType) End() token.Pos

func (*StructType) Pos

func (x *StructType) Pos() token.Pos

type SwitchStmt

SwitchStmt节点表示表达式switch语句.

type SwitchStmt struct {
    Switch token.Pos  // position of "switch" keyword
    Init   Stmt       // initialization statement; or nil
    Tag    Expr       // tag expression; or nil
    Body   *BlockStmt // CaseClauses only
}

func (*SwitchStmt) End

func (s *SwitchStmt) End() token.Pos

func (*SwitchStmt) Pos

func (s *SwitchStmt) Pos() token.Pos

type TypeAssertExpr

TypeAssertExpr节点表示一个表达式,后跟一个类型声明.

type TypeAssertExpr struct {
    X      Expr      // expression
    Lparen token.Pos // position of "("; added in Go 1.2
    Type   Expr      // asserted type; nil means type switch X.(type)
    Rparen token.Pos // position of ")"; added in Go 1.2
}

func (*TypeAssertExpr) End

func (x *TypeAssertExpr) End() token.Pos

func (*TypeAssertExpr) Pos

func (x *TypeAssertExpr) Pos() token.Pos

type TypeSpec

TypeSpec节点表示类型声明(TypeSpec生产).

type TypeSpec struct {
    Doc     *CommentGroup // associated documentation; or nil
    Name    *Ident        // type name
    Assign  token.Pos     // position of '=', if any; added in Go 1.9
    Type    Expr          // *Ident, *ParenExpr, *SelectorExpr, *StarExpr, or any of the *XxxTypes
    Comment *CommentGroup // line comments; or nil
}

func (*TypeSpec) End

func (s *TypeSpec) End() token.Pos

func (*TypeSpec) Pos

func (s *TypeSpec) Pos() token.Pos

type TypeSwitchStmt

TypeSwitchStmt节点表示类型切换语句.

type TypeSwitchStmt struct {
    Switch token.Pos  // position of "switch" keyword
    Init   Stmt       // initialization statement; or nil
    Assign Stmt       // x := y.(type) or y.(type)
    Body   *BlockStmt // CaseClauses only
}

func (*TypeSwitchStmt) End

func (s *TypeSwitchStmt) End() token.Pos

func (*TypeSwitchStmt) Pos

func (s *TypeSwitchStmt) Pos() token.Pos

type UnaryExpr

UnaryExpr节点表示一元表达式. 一元" *"表达式通过StarExpr节点表示.

type UnaryExpr struct {
    OpPos token.Pos   // position of Op
    Op    token.Token // operator
    X     Expr        // operand
}

func (*UnaryExpr) End

func (x *UnaryExpr) End() token.Pos

func (*UnaryExpr) Pos

func (x *UnaryExpr) Pos() token.Pos

type ValueSpec

ValueSpec节点表示常量或变量声明(ConstSpec或VarSpec生产).

type ValueSpec struct {
    Doc     *CommentGroup // associated documentation; or nil
    Names   []*Ident      // value names (len(Names) > 0)
    Type    Expr          // value type; or nil
    Values  []Expr        // initial values; or nil
    Comment *CommentGroup // line comments; or nil
}

func (*ValueSpec) End

func (s *ValueSpec) End() token.Pos

func (*ValueSpec) Pos

func (s *ValueSpec) Pos() token.Pos

type Visitor

将为Walk遇到的每个节点调用一个Visiter的Visit方法. 如果结果访问者w不为nil,则Walk用访问者w访问节点的每个子节点,然后调用w.Visit(nil).

type Visitor interface {
    Visit(node Node) (w Visitor)
}

by  ICOPY.SITE