目录 搜索
archive archive/tar archive/zip bufio bufio(缓存) builtin builtin(内置包) bytes bytes(包字节) compress compress/bzip2(压缩/bzip2) compress/flate(压缩/flate) compress/gzip(压缩/gzip) compress/lzw(压缩/lzw) compress/zlib(压缩/zlib) container container/heap(容器数据结构heap) container/list(容器数据结构list) container/ring(容器数据结构ring) context context(上下文) crypto crypto(加密) crypto/aes(加密/aes) crypto/cipher(加密/cipher) crypto/des(加密/des) crypto/dsa(加密/dsa) crypto/ecdsa(加密/ecdsa) crypto/elliptic(加密/elliptic) crypto/hmac(加密/hmac) crypto/md5(加密/md5) crypto/rand(加密/rand) crypto/rc4(加密/rc4) crypto/rsa(加密/rsa) crypto/sha1(加密/sha1) crypto/sha256(加密/sha256) crypto/sha512(加密/sha512) crypto/subtle(加密/subtle) crypto/tls(加密/tls) crypto/x509(加密/x509) crypto/x509/pkix(加密/x509/pkix) database database/sql(数据库/sql) database/sql/driver(数据库/sql/driver) debug debug/dwarf(调试/dwarf) debug/elf(调试/elf) debug/gosym(调试/gosym) debug/macho(调试/macho) debug/pe(调试/pe) debug/plan9obj(调试/plan9obj) encoding encoding(编码) encoding/ascii85(编码/ascii85) encoding/asn1(编码/asn1) encoding/base32(编码/base32) encoding/base64(编码/base64) encoding/binary(编码/binary) encoding/csv(编码/csv) encoding/gob(编码/gob) encoding/hex(编码/hex) encoding/json(编码/json) encoding/pem(编码/pem) encoding/xml(编码/xml) errors errors(错误) expvar expvar flag flag(命令行参数解析flag包) fmt fmt go go/ast(抽象语法树) go/build go/constant(常量) go/doc(文档) go/format(格式) go/importer go/parser go/printer go/scanner(扫描仪) go/token(令牌) go/types(类型) hash hash(散列) hash/adler32 hash/crc32 hash/crc64 hash/fnv html html html/template(模板) image image(图像) image/color(颜色) image/color/palette(调色板) image/draw(绘图) image/gif image/jpeg image/png index index/suffixarray io io io/ioutil log log log/syslog(日志系统) math math math/big math/big math/bits math/bits math/cmplx math/cmplx math/rand math/rand mime mime mime/multipart(多部分) mime/quotedprintable net net net/http net/http net/http/cgi net/http/cookiejar net/http/fcgi net/http/httptest net/http/httptrace net/http/httputil net/http/internal net/http/pprof net/mail net/mail net/rpc net/rpc net/rpc/jsonrpc net/smtp net/smtp net/textproto net/textproto net/url net/url os os os/exec os/signal os/user path path path/filepath(文件路径) plugin plugin(插件) reflect reflect(反射) regexp regexp(正则表达式) regexp/syntax runtime runtime(运行时) runtime/debug(调试) runtime/internal/sys runtime/pprof runtime/race(竞争) runtime/trace(执行追踪器) sort sort(排序算法) strconv strconv(转换) strings strings(字符串) sync sync(同步) sync/atomic(原子操作) syscall syscall(系统调用) testing testing(测试) testing/iotest testing/quick text text/scanner(扫描文本) text/tabwriter text/template(定义模板) text/template/parse time time(时间戳) unicode unicode unicode/utf16 unicode/utf8 unsafe unsafe
文字

  • 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 mainimport ("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 stringswitch x := n.(type) {case *ast.BasicLit:
			s = x.Valuecase *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 mainimport ("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 mainimport ("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.Bufferif 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)}
上一篇: 下一篇: