非常教程

Go参考手册

go

go/ast(抽象语法树)

  • import "go/ast"
  • 概述
  • 索引
  • 示例

概述

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

索引

  • 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

示例

CommentMap Inspect Print

包文件

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

func FileExports(查看源代码)

func FileExports(src *File) bool

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

FileExports 报告是否有导出声明。

func FilterDecl(查看源代码)

func FilterDecl(decl Decl, f Filter) bool

FilterDecl 通过删除不通过过滤器f的所有名称(包括struct 字段和接口方法名称,但不包括参数列表)来修饰 AST 的 Go 声明。

FilterDecl 报告过滤后是否有任何声明的名称。

func FilterFile(查看源代码)

func FilterFile(src *File, f Filter) bool

FilterFile 通过删除不通过过滤器f的顶级声明(包括 struct字段和接口方法名称,但不包含参数列表)中的所有名称来修剪 AST 文件。如果后面的声明为空,则声明将从 AST 中删除。导入声明总是被删除。File.Comments 列表不会更改。

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

func FilterPackage(查看源代码)

func FilterPackage(pkg *Package, f Filter) bool

FilterPackage 通过删除不通过过滤器f的顶级声明(包括struct 字段和接口方法名称,但不包括参数列表)中的所有名称来修剪适用于 Go 包的 AST。如果后面的声明为空,则声明将从 AST 中删除。pkg.Files 列表未更改,因此文件名和顶级包注释不会丢失。

FilterPackage 报告过滤后是否有任何顶级声明。

func Fprint(查看源代码)

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

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

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

func Inspect(查看源代码)

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

检查以深度优先顺序遍历AST:它通过调用f(node) 开始;节点不能为零。如果 f 返回 true,Inspect 会为节点的每个非零子节点递归调用f,然后调用 f(nil)。

示例

这个例子演示了如何检查 Go 程序的 AST。

package main

import (
	"fmt"
	"go/ast"
	"go/parser"
	"go/token"
)

func main() {
	// src是我们要检查AST的输入。
	src := `
package p
const c = 1.0
var X = f(3.14)*2 + c
`

	// 通过解析src来创建AST。
	fset := token.NewFileSet() // 职位相对于fset
	f, err := parser.ParseFile(fset, "src.go", src, 0)
	if err != nil {
		panic(err)
	}

	// 检查AST并打印所有标识符和文字。
	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

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到标准输出,跳过零字段。打印(fset,x)与Fprint(os.Stdout,fset,x,NotNilFilter)相同。

示例

这个例子显示了打印进行调试时 AST 的外观。

package main

import (
	"go/ast"
	"go/parser"
	"go/token"
)

func main() {
	// src是我们要打印AST的输入。
	src := `
package main
func main() {
	println("Hello, World!")
}
`

	// 通过解析src来创建AST。
	fset := token.NewFileSet() // 职位相对于fset
	f, err := parser.ParseFile(fset, "", src, 0)
	if err != nil {
		panic(err)
	}

	// 打印AST。
	ast.Print(fset, f)

}

func SortImports(显示源代码)

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

SortImports 对f中的导入块中的连续导入行进行排序。它还可以在不丢失数据的情况下删除重复导入。

func Walk(显示源代码)

func Walk(v Visitor, node Node)

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

type ArrayType(显示源代码)

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

type ArrayType struct {
        Lbrack token.Pos // “[”的位置
        Len    Expr      // [...] T数组类型的省略号节点,切片类型为零
        Elt    Expr      // 元素类型
}

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   // Tok的位置
        Tok    token.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 // 不良声明的位置范围
}

func (*BadDecl) End(显示源代码)

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

func (*BadDecl) Pos(显示源代码)

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

type BadExpr(显示源代码)

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

type BadExpr struct {
        From, To token.Pos // 不良表达的位置范围
}

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 // 不良陈述的位置范围
}

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        // 左操作数
        OpPos token.Pos   // Op的位置
        Op    token.Token // 操作者
        Y     Expr        // 右操作数
}

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 // “{”的位置
        List   []Stmt
        Rbrace token.Pos // “}”的位置
}

func (*BlockStmt) End(显示源代码)

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

func (*BlockStmt) Pos(显示源代码)

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

type BranchStmt(显示源代码)

BranchStmt 节点表示中断,继续,转到或直通语句。

type BranchStmt struct {
        TokPos token.Pos   // Tok的位置
        Tok    token.Token // 关键字令牌(BREAK,CONTINUE,GOTO,FALLTHROUGH)
        Label  *Ident      // 标签名称; 或者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      // 函数表达
        Lparen   token.Pos // “(”的位置
        Args     []Expr    // 函数参数; 或者没有
        Ellipsis token.Pos // “......”的位置(如果没有“......”则为token.NoPos)
        Rparen   token.Pos // “)的位置
}

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 // “case”或“default”关键字的位置
        List  []Expr    // 表达式或类型列表; nil表示默认情况
        Colon token.Pos // “:”的位置
        Body  []Stmt    // 声明清单; 或者没有
}

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 // “chan”关键字或“< - ”的位置(以先到者为准)
        Arrow token.Pos // “< - ”的位置(如果没有“< - ”则为token.NoPos)
        Dir   ChanDir   // 渠道方向
        Value Expr      // 值类型
}

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 // “case”或“default”关键字的位置
        Comm  Stmt      // 发送或接收声明; nil表示默认情况
        Colon token.Pos // “:”的位置
        Body  []Stmt    // 声明清单; 或者没有
}

func (*CommClause) End(显示源代码)

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

func (*CommClause) Pos(显示源代码)

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

type Comment(显示源代码)

Comment 节点表示一个 //style 或 /*- style 的注释。

type Comment struct {
        Slash token.Pos // “/”开头注释的位置
        Text  string    // 注释文本(不包括用于//style 注释的'\n')
}

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(显示源代码)

CommentMap 将 AST 节点映射到与其关联的评论组列表。请参阅 NewCommentMap 以获取关联的描述。

type CommentMap map[Node][]*CommentGroup

示例

这个例子说明了如何使用 ast.CommentMap 去除 Go 程序中的变量声明,同时保持正确的评论关联。

package main

import (
	"bytes"
	"fmt"
	"go/ast"
	"go/format"
	"go/parser"
	"go/token"
)

func main() {
	// src是我们创建AST的输入
	// 将要操纵。
	src := `
// 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 foo variable.
var foo = hello // line comment 2 

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

	// 通过解析src来创建AST。
	fset := token.NewFileSet() // 职位相对于fset
	f, err := parser.ParseFile(fset, "src.go", src, parser.ParseComments)
	if err != nil {
		panic(err)
	}

	// 从ast.File的注释中创建一个ast.CommentMap。
	// 这有助于保持注释之间的关联
	// 和AST节点。
	cmap := ast.NewCommentMap(fset, f, f.Comments)

	// 从声明列表中删除第一个变量声明。
	for i, decl := range f.Decls {
		if gen, ok := decl.(*ast.GenDecl); ok && gen.Tok == token.VAR {
			copy(f.Decls[i:], f.Decls[i+1:])
			f.Decls = f.Decls[:len(f.Decls)-1]
		}
	}

	// 使用注释地图过滤不再属于的注释
	// (与变量声明相关的注释),并创建
	// 新注释列表。
	f.Comments = cmap.Filter(f).Comments()

	// 打印修改后的AST。
	var buf bytes.Buffer
	if err := format.Node(&buf, fset, f); err != nil {
		panic(err)
	}
	fmt.Printf("%s", buf.Bytes())

}

func NewCommentMap(显示源代码)

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

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

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

 -  g与n结束时在同一行开始
-  g紧跟在n之后的行上开始,并且有
  g之后和下一个节点之前至少有一个空行
-  g在n之前开始,并且不与n之前的节点相关联
  通过以前的规则

NewCommentMap 尝试将注释组关联到可能的“largest”节点:例如,如果注释是一个跟随分配的行注释,则注释与整个分配相关联,而不仅仅是分配中的最后一个操作数。

func (CommentMap) Comments(显示源代码)

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

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

func (CommentMap) Filter(显示源代码)

func (cmap CommentMap) Filter(node Node) CommentMap

筛选器返回一个新的注释映射,其中只包含由节点指定的 AST 中存在相应节点的 cmap 的条目。

func (CommentMap) String(显示源代码)

func (cmap CommentMap) String() string

func (CommentMap) Update(显示源代码)

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

更新用新节点替换评论映射中的旧节点并返回新节点。与旧节点相关联的注释与新节点相关联。

type CompositeLit(显示源代码)

CompositeLit 节点表示复合文字。

type CompositeLit struct {
        Type   Expr      // 字面类型; 或者nil
        Lbrace token.Pos // “{”的位置
        Elts   []Expr    // 复合元素列表; 或者nil
        Rbrace token.Pos // “}”的位置
}

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
        // 包含过滤或未导出的方法
}

type DeclStmt(显示源代码)

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

type DeclStmt struct {
        Decl Decl // * GenDecl与CONST,TYPE或VAR令牌
}

func (*DeclStmt) End(显示源代码)

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

func (*DeclStmt) Pos(显示源代码)

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

type DeferStmt(显示源代码)

DeferStmt 节点表示推迟语句。

type DeferStmt struct {
        Defer token.Pos // “defer”关键字的位置
        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 // “...”的位置
        Elt      Expr      // 省略号元素类型(仅限参数列表); 或者没有

func (*Ellipsis) End(显示源代码)

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

func (*Ellipsis) Pos(显示源代码)

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

type EmptyStmt(显示源代码)

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

type EmptyStmt struct {
        Semicolon token.Pos // 以下“;”的位置
        Implicit  bool      // 如果设置,“;” 在源中省略了
}

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
        // 包含过滤或未导出的方法
}

type ExprStmt(显示源代码)

ExprStmt节点表示语句列表中的(stand-alone)表达式。

type ExprStmt struct {
        X Expr // 表达式
}

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 // 相关文件; 或者没有
        Names   []*Ident      // 字段/方法/参数名称;或者如果匿名字段为零
        Type    Expr          // 字段/方法/参数类型
        Tag     *BasicLit     // 字段标签; 或者没有
        Comment *CommentGroup // 行注释; 或者没有
}

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 // 开括号/括号的位置,如果有的话
        List    []*Field  // 字段列表; 或者没有
        Closing token.Pos // 右括号/括号的位置,如果有的话
}

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(显示源代码)

一个文件节点代表一个 Go 源文件。

注释列表按照外观顺序包含源文件中的所有注释,包括通过 Doc 和注释字段从其他节点指向的注释。

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

注释是否以及如何与节点相关取决于操作程序对语法树的解释:除了与节点直接相关的文档和评论评论之外,其余注释是“自由浮动的(free-floating)”(另见issues #18593, #20744)。

type File struct {
        Doc        *CommentGroup   // 相关文件; 或者没有
        Package    token.Pos       // “package”关键字的位置
        Name       *Ident          // 包名称
        Decls      []Decl          // 顶级声明; 或者没有
        Scope      *Scope          // 包范围(仅限此文件)
        Imports    []*ImportSpec   // 在此文件中导入
        Unresolved []*Ident        // 此文件中未解析的标识符
        Comments   []*CommentGroup // 源文件中所有注释的列表
}

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 // “for”关键字的位置
        Init Stmt      // 初始化语句; 或者没有
        Cond Expr      // 条件; 或者没有
        Post Stmt      // post iteration语句; 或者没有
        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 // 相关文件; 或者没有
        Recv *FieldList    // 接收器(方法); 或零(函数)
        Name *Ident        // 函数/方法名称
        Type *FuncType     // 函数签名:“func”关键字的参数,结果和位置
        Body *BlockStmt    // 功能体; 或者对于外部(非Go)功能为零

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  // 函数类型
        Body *BlockStmt // 函数体
}

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  // “func”关键字的位置(如果没有“func”,则为token.NoPos)
        Params  *FieldList // (传入)参数;非空
        Results *FieldList // (外向)结果;或者没有
}

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 // 相关文件; 或者没有
        TokPos token.Pos     // Tok 的位置
        Tok    token.Token   // IMPORT, CONST, TYPE, VAR
        Lparen token.Pos     // “(',如果有的话)的位置
        Specs  []Spec
        Rparen token.Pos // ')'的位置,如果有的话
}

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 // “go” 关键字的位置
        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 // 确定位置
        Name    string    // 标识符名称
        Obj     *Object   // 表示对象; 或者没有
}

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(显示源代码)

IfStmt 节点表示一个 if 语句。

type IfStmt struct {
        If   token.Pos // “if”关键字的位置
        Init Stmt      // 初始化语句; 或者没有
        Cond Expr      // 条件
        Body *BlockStmt
        Else Stmt // 别的分支; 或者没有
}

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 // 相关文件; 或者没有
        Name    *Ident        // 本地包名(包括“.”); 或者没有
        Path    *BasicLit     // 导入路径
        Comment *CommentGroup // 行注释;或者没有
        EndPos  token.Pos     // 规范结束(如果非零则覆盖Path.Pos)
}

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   // Tok的位置
        Tok    token.Token // INC或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      // 表达式
        Lbrack token.Pos // “[”的位置
        Index  Expr      // 索引表达式
        Rbrack token.Pos // “]的位置
}

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  // “interface”关键字的位置
        Methods    *FieldList // 方法列表
        Incomplete bool       // 如果“方法”列表中缺少(源)方法,则为true
}

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 // “:”的位置
        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 // “:”的位置
        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 // “map”关键字的位置
        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 (
        // 如果设置,则排除重复的函数声明。
        FilterFuncDuplicates MergeMode = 1 << iota
        // 如果设置,则与特定的注释无关
        // AST节点(作为Doc或Comment)被排除在外。
        FilterUnassociatedComments
        // 如果设置,则排除重复的导入声明。
        FilterImportDuplicates
)

type Node(显示源代码)

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

type Node interface {
        Pos() token.Pos // 属于该节点的第一个字符的位置
        End() token.Pos // 紧接在节点之后的第一个字符的位置
}

type ObjKind(显示源代码)

ObjKind 描述了一个对象代表什么。

type ObjKind int

可能的对象种类的列表。

const (
        Bad ObjKind = iota // 用于错误处理
        Pkg                // 包
        Con                // 常量
        Typ                // 类型
        Var                // 变量
        Fun                // 函数或方法
        Lbl                // 标签
)

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      // 声明的名称
        Decl interface{} // 对应的Field,XxxSpec,FuncDecl,LabeledStmt,AssignStmt,Scope; 或者没有
        Data interface{} // 特定对象数据;或者没有
        Type interface{} // 占位符的类型信息; 可能是零
}

func NewObj(显示源代码)

func NewObj(kind ObjKind, name string) *Object

NewObj 创建一个给定种类和名称的新对象。

func (*Object) Pos(显示源代码)

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

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

type Package(显示源代码)

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

type Package struct {
        Name    string             // 包名称
        Scope   *Scope             // 所有文件的包范围
        Imports map[string]*Object // 包id的地图 - >包对象
        Files   map[string]*File   // 按文件名转到源文件

func NewPackage(显示源代码)

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

NewPackage 从一组 File 节点创建一个新的 Package 节点。它解决跨文件的未解析标识符,并相应地更新每个文件的未解析列表。如果提供了非零导入器和 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 // “(”的位置
        X      Expr      // 括号表达式
        Rparen token.Pos // “)”的位置
}

func (*ParenExpr) End(显示源代码)

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

func (*ParenExpr) Pos(显示源代码)

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

type RangeStmt(显示源代码)

RangeStmt 表示带有范围子句的 for 语句。

type RangeStmt struct {
        For        token.Pos   // “for”关键字的位置
        Key, Value Expr        // 关键,值可能是零
        TokPos     token.Pos   // Tok的位置; 如果Key == nil则无效
        Tok        token.Token // 如果Key == nil,ASSIGN,DEFINE,则为ILLEGAL
        X          Expr        // 值范围超过
        Body       *BlockStmt
}

func (*RangeStmt) End(显示源代码)

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

func (*RangeStmt) Pos(显示源代码)

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

type ReturnStmt(显示源代码)

ReturnStmt 节点表示一个返回语句。

type ReturnStmt struct {
        Return  token.Pos // “return”关键字的位置
        Results []Expr    // 结果表达式;或者没有
}

func (*ReturnStmt) End(显示源代码)

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

func (*ReturnStmt) Pos(显示源代码)

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

type Scope(显示源代码)

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 插入到作用域中。如果范围已经包含名称相同的对象 alt,则 Insert 会保持范围不变并返回 alt。否则,它插入 obj 并返回 nil。

func (*Scope) Lookup(显示源代码)

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

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

func (*Scope) String(显示源代码)

func (s *Scope) String() string

调试支持

type SelectStmt(显示源代码)

SelectStmt 节点表示一个 select 语句。

type SelectStmt struct {
        Select token.Pos  // “select”关键字的位置
        Body   *BlockStmt // 仅限CommClauses
}

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   // 表达式
        Sel *Ident // 场选择器
}

func (*SelectorExpr) End(显示源代码)

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

func (*SelectorExpr) Pos(显示源代码)

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

type SendStmt(显示源代码)

SendStmt 节点表示发送语句。

type SendStmt struct {
        Chan  Expr
        Arrow token.Pos // “<- ”的位置
        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      // 表达式
        Lbrack token.Pos // “[”的位置
        Low    Expr      // 切片范围的开始;或者没有
        High   Expr      // 切片范围的结束; 或者没有
        Max    Expr      // 最大切片容量; 或者没有
        Slice3 bool      // 如果3-index切片(存在2个冒号),则为true
        Rbrack token.Pos // “]"的位置
}

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
        // 包含过滤或未导出的方法
}

type StarExpr(显示源代码)

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

type StarExpr struct {
        Star token.Pos // “*”的位置
        X    Expr      // 操作数
}

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
        // 包含过滤或未导出的方法
}

type StructType(显示源代码)

StructType 节点表示一个结构类型。

type StructType struct {
        Struct     token.Pos  // “struct”关键字的位置
        Fields     *FieldList // 列表声明清单
        Incomplete bool       // 如果“字段”列表中缺少(源)字段,则为true
}

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  // “switch”关键字的位置
        Init   Stmt       // 初始化语句; 或者没有
        Tag    Expr       // 标签表达; 或者没有
        Body   *BlockStmt // 仅限CaseClauses
}

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      // 表达式
        Lparen token.Pos // “(”的位置
        Type   Expr      // 断言类型; nil表示类型开关X.(type)
        Rparen token.Pos // “)"的位置
}

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 // 相关文件;或者没有
        Name    *Ident        // 类型名称
        Assign  token.Pos     // '='的位置,如果有的话
        Type    Expr          // *Ident,*ParenExpr,*SelectorExpr,*StarExpr或任何*XxxTypes
        Comment *CommentGroup // 行注释;或者没有
}

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  // “switch”关键字的位置
        Init   Stmt       // 初始化语句; 或者没有
        Assign Stmt       // x := y.(type)或y.(type)
        Body   *BlockStmt // 仅限CaseClauses
}

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   // Op 的位置
        Op    token.Token // 操作者
        X     Expr        // 操作数
}

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 // 相关文件; 或者没有
        Names   []*Ident      // 值名称(len(Names) > 0)
        Type    Expr          // 值类型; 或者没有
        Values  []Expr        // 初始值; 或者没有
        Comment *CommentGroup // 行注释 或者没有
}

func (*ValueSpec) End(显示源代码)

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

func (*ValueSpec) Pos(显示源代码)

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

type Visitor(显示源代码)

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

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

Go 是一种编译型语言,它结合了解释型语言的游刃有余,动态类型语言的开发效率,以及静态类型的安全性。它也打算成为现代的,支持网络与多核计算的语言。要满足这些目标,需要解决一些语言上的问题:一个富有表达能力但轻量级的类型系统,并发与垃圾回收机制,严格的依赖规范等等。这些无法通过库或工具解决好,因此Go也就应运而生了。

主页 https://golang.org/
源码 https://go.googlesource.com/go
发布版本 1.9.2

Go目录

1.档案 | archive
2.缓冲区 | bufio
3.内置 | builtin
4.字节 | bytes
5.压缩 | compress
6.容器 | container
7.上下文 | context
8.加密 | crypto
9.数据库 | database
10.调试 | debug
11.编码 | encoding
12.错误 | errors
13. expvar
14.flag
15. fmt
16. go
17.散列 | hash
18.html
19.图像 | image
20.索引 | index
21.io
22.日志 | log
23.数学 | math
24. math/big
25.math/bits
26.math/cmplx
27.math/rand
28.拟态 | mime
29.net
30.net/http
31. net/mail
32. net/rpc
33.net/smtp
34. net/textproto
35. net/url
36.os
37.路径 | path
38.插件 | plugin
39.反射 | reflect
40.正则表达式 | regexp
41.运行时 | runtime
42.排序算法 | sort
43.转换 | strconv
44.字符串 | strings
45.同步 | sync
46.系统调用 | syscall
47.测试 | testing
48.文本 | text
49.时间戳 | time
50.unicode
51.不安全性 | unsafe
52.Go 语言数据类型
53.Go 语言基础语法
54.Go 语言结构
55.Go 语言 select 语句
56.Go 语言 switch 语句
57.Go 语言 if 语句嵌套
58.Go 语言 if…else 语句
59.Go 语言 if 语句
60.Go 语言运算符
61.Go 语言常量
62.Go 语言函数闭包
63.Go 语言函数作为实参
64.Go 语言函数引用传递值
65.Go 语言函数值传递值
66.Go 语言函数
67.Go 语言 goto 语句
68.Go 语言 continue 语句
69.Go 语言 break 语句
70.Go 语言循环嵌套
71.Go 语言 for 循环
72.Go 语言结构体
73.Go 语言指针作为函数参数
74.Go 语言指向指针的指针
75.Go 语言指针数组
76.Go 语言指针
77.Go 语言向函数传递数组
78.Go 语言多维数组
79.Go 语言变量作用域
80.Go 语言函数方法
81.Go 错误处理
82.Go 语言接口
83.Go 语言类型转换
84.Go 语言递归函数
85.Go 语言Map(集合)
86.Go 语言范围(Range)
87.Go 语言切片(Slice)
88.Go 并发
89.Go fmt.Sprintf 格式化字符串