Golang标准库——go(1)

DevilRoshan · · 667 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

  • ast

ast

ast包声明用于表示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报告过滤后是否还有任何顶级声明。

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)。

func main() {
    // src is the input for which we want to inspect the AST.
    src := `
package p
const c = 1.0
var X = f(3.14)*2 + c
`

    // Create the AST by parsing src.
    fset := token.NewFileSet() // positions are relative to fset
    f, err := parser.ParseFile(fset, "src.go", src, 0)
    if err != nil {
        panic(err)
    }

    // Inspect the AST and print all identifiers and literals.
    ast.Inspect(f, func(n ast.Node) bool {
        var s string
        switch x := n.(type) {
        case *ast.BasicLit:
            s = x.Value
        case *ast.Ident:
            s = x.Name
        }
        if s != "" {
            fmt.Printf("%s:\t%s\n", fset.Position(n.Pos()), s)
        }
        return true
    })
}

func IsExported

func IsExported(name string) bool

IsExported报告名称是否为导出的Go符号(即,是否以大写字母开头)。

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)相同。

func main() {
    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)
}

func SortImports

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

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

func Walk

func Walk(v Visitor, node Node)

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

type 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
}

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

func (*ArrayType) End

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

func (*ArrayType) Pos

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

type AssignStmt

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

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

func (*AssignStmt) End

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

func (*AssignStmt) Pos

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

type BadDecl

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

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

func (*BadDecl) End

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

func (*BadDecl) Pos

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

type BadExpr

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

BadExpr节点是包含语法错误的表达式的占位符,无法为其创建正确的表达式节点。

func (*BadExpr) End

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

func (*BadExpr) Pos

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

type BadStmt

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

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

func (*BadStmt) End

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

func (*BadStmt) Pos

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

type 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`
}

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

func (*BasicLit) End

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

func (*BasicLit) Pos

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

type BinaryExpr

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

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

func (*BinaryExpr) End

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

func (*BinaryExpr) Pos

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

type BlockStmt

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

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

func (*BlockStmt) End

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

func (*BlockStmt) Pos

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

type BranchStmt

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

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

func (*BranchStmt) End

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

func (*BranchStmt) Pos

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

type 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 ")"
}

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

func (*CallExpr) End

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

func (*CallExpr) Pos

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

type 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
}

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

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

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 "<-")
    Dir   ChanDir   // channel direction
    Value Expr      // value type
}

ChanType节点表示通道类型。

func (*ChanType) End

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

func (*ChanType) Pos

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

type 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
}

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

func (*CommClause) End

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

func (*CommClause) Pos

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

type Comment

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

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

func (*Comment) End

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

func (*Comment) Pos

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

type CommentGroup

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

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

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

type CommentMap map[Node][]*CommentGroup

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

func NewCommentMap

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

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

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

func (CommentMap) Filter

func (cmap CommentMap) Filter(node Node) CommentMap

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

func (CommentMap)

func (cmap CommentMap) String() string

func (CommentMap) Update

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

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

type 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 "}"
}

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

func (*CompositeLit) End

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

func (*CompositeLit) Pos

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

type Decl

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

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

type DeclStmt

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

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

func (*DeclStmt) End

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

func (*DeclStmt) Pos

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

type DeferStmt

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

DeferStmt节点表示defer语句。

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
}

Ellipsis节点代表参数列表中的“ ...”类型或数组类型中的“ ...”长度。

func (*Ellipsis) End

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

func (*Ellipsis) Pos

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

type EmptyStmt

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

EmptyStmt节点表示一个空语句。 空语句的“位置”是紧接其后(显式或隐式)分号的位置。

func (*EmptyStmt) End

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

func (*EmptyStmt) Pos

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

type Expr

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

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

type ExprStmt

type ExprStmt struct {
    X Expr // expression
}

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

func (*ExprStmt) End

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

func (*ExprStmt) Pos

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

type Field

type Field struct {
    Doc     *CommentGroup // associated documentation; or nil
    Names   []*Ident      // field/method/parameter names; or nil if anonymous field
    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

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

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

type 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
}

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

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

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
}

File节点代表Go源文件。

“注释”列表按出现顺序包含源文件中的所有注释,包括通过“文档”和“注释”字段从其他节点指向的注释。

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

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
}

ForStmt代表for语句。

func (*ForStmt) End

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

func (*ForStmt) Pos

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

type 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 (forward declaration)
}

FuncDecl节点表示函数声明。

func (*FuncDecl) End

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

func (*FuncDecl) Pos

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

type FuncLit

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

FuncLit节点表示函数文字。

func (*FuncLit) End

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

func (*FuncLit) Pos

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

type 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
}

FuncType节点代表函数类型。

func (*FuncType) End

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

func (*FuncType) Pos

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

type GenDecl

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
}

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

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

token.IMPORT  *ImportSpec
token.CONST   *ValueSpec
token.TYPE    *TypeSpec
token.VAR     *ValueSpec

func (*GenDecl) End

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

func (*GenDecl) Pos

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

type GoStmt

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

GoStmt节点表示go语句。

func (*GoStmt) End

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

func (*GoStmt) Pos

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

type Ident

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

Ident节点代表一个标识符。

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是否为导出的Go符号(即是否以大写字母开头)。

func (*Ident) Pos

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

func (*Ident) String

func (id *Ident) String() string

type IfStmt

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
}

IfStmt节点表示一个if语句。

func (*IfStmt) End

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

func (*IfStmt) Pos

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

type 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)
}

ImportSpec节点表示单个包导入。

func (*ImportSpec) End

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

func (*ImportSpec) Pos

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

type Importer

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

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

type IncDecStmt

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

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

func (*IncDecStmt) End

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

func (*IncDecStmt) Pos

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

type IndexExpr

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

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

func (*IndexExpr) End

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

func (*IndexExpr) Pos

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

type 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
}

InterfaceType节点表示接口类型。

func (*InterfaceType) End

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

func (*InterfaceType) Pos

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

type KeyValueExpr

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

KeyValueExpr节点表示复合文字中的(key:value)对。

func (*KeyValueExpr) End

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

func (*KeyValueExpr) Pos

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

type LabeledStmt

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

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

func (*LabeledStmt) End

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

func (*LabeledStmt) Pos

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

type MapType

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

MapType节点代表Map类型。

func (*MapType) End

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

func (*MapType) Pos

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

type MergeMode

type MergeMode uint

MergeMode标志控制MergePackageFiles的行为。

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

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
}

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

type ObjKind

type ObjKind int

ObjKind描述对象代表什么。

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

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
}

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

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

Kind    Data type         Data value
Pkg     *Scope            package scope
Con     int               iota for the respective declaration

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

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
}

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

func NewPackage

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

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

func (*Package) End

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

func (*Package) Pos

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

type ParenExpr

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

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

func (*ParenExpr) End

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

func (*ParenExpr) Pos

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

type RangeStmt

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
}

RangeStmt表示带有range子句的for语句。

func (*RangeStmt) End

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

func (*RangeStmt) Pos

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

type ReturnStmt

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

ReturnStmt节点代表一个return语句。

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)

Insert尝试将命名对象obj插入范围s中。 如果作用域已经包含具有相同名称的对象alt,则Insert将使作用域保持不变并返回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

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

An SelectStmt node represents a select statement.

func (*SelectStmt) End

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

func (*SelectStmt) Pos

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

type SelectorExpr

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

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

func (*SelectorExpr) End

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

func (*SelectorExpr) Pos

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

type SendStmt

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

SendStmt节点代表一个send语句。

func (*SendStmt) End

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

func (*SendStmt) Pos

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

type 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
    Slice3 bool      // true if 3-index slice (2 colons present)
    Rbrack token.Pos // position of "]"
}

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

func (*SliceExpr) End

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

func (*SliceExpr) Pos

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

type Spec

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

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

type StarExpr

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

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

func (*StarExpr) End

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

func (*StarExpr) Pos

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

type Stmt

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

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

type 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
}

StructType节点表示结构类型。

func (*StructType) End

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

func (*StructType) Pos

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

type SwitchStmt

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
}

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

func (*SwitchStmt) End

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

func (*SwitchStmt) Pos

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

type TypeAssertExpr

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

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

func (*TypeAssertExpr) End

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

func (*TypeAssertExpr) Pos

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

type TypeSpec

type TypeSpec struct {
    Doc     *CommentGroup // associated documentation; or nil
    Name    *Ident        // type name
    Type    Expr          // *Ident, *ParenExpr, *SelectorExpr, *StarExpr, or any of the *XxxTypes
    Comment *CommentGroup // line comments; or nil
}

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

func (*TypeSpec) End

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

func (*TypeSpec) Pos

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

type 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
}

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

func (*TypeSwitchStmt) End

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

func (*TypeSwitchStmt) Pos

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

type UnaryExpr

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

UnaryExpr节点表示一元表达式。 一元“ *”表达式通过StarExpr节点表示。

func (*UnaryExpr) End

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

func (*UnaryExpr) Pos

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

type ValueSpec

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
}

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

func (*ValueSpec) End

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

func (*ValueSpec) Pos

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

type Visitor

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

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


有疑问加站长微信联系(非本文作者)

本文来自:简书

感谢作者:DevilRoshan

查看原文:Golang标准库——go(1)

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

667 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传