亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

directory search
archive archive/tar archive/zip bufio bufio(緩存) builtin builtin(內(nèi)置包) bytes bytes(包字節(jié)) compress compress/bzip2(壓縮/bzip2) compress/flate(壓縮/flate) compress/gzip(壓縮/gzip) compress/lzw(壓縮/lzw) compress/zlib(壓縮/zlib) container container/heap(容器數(shù)據(jù)結(jié)構(gòu)heap) container/list(容器數(shù)據(jù)結(jié)構(gòu)list) container/ring(容器數(shù)據(jù)結(jié)構(gòu)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(數(shù)據(jù)庫(kù)/sql) database/sql/driver(數(shù)據(jù)庫(kù)/sql/driver) debug debug/dwarf(調(diào)試/dwarf) debug/elf(調(diào)試/elf) debug/gosym(調(diào)試/gosym) debug/macho(調(diào)試/macho) debug/pe(調(diào)試/pe) debug/plan9obj(調(diào)試/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(錯(cuò)誤) expvar expvar flag flag(命令行參數(shù)解析flag包) fmt fmt go go/ast(抽象語(yǔ)法樹) 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(調(diào)色板) image/draw(繪圖) image/gif image/jpeg image/png index index/suffixarray io io io/ioutil log log log/syslog(日志系統(tǒng)) 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(正則表達(dá)式) regexp/syntax runtime runtime(運(yùn)行時(shí)) runtime/debug(調(diào)試) runtime/internal/sys runtime/pprof runtime/race(競(jìng)爭(zhēng)) runtime/trace(執(zhí)行追蹤器) sort sort(排序算法) strconv strconv(轉(zhuǎn)換) strings strings(字符串) sync sync(同步) sync/atomic(原子操作) syscall syscall(系統(tǒng)調(diào)用) testing testing(測(cè)試) testing/iotest testing/quick text text/scanner(掃描文本) text/tabwriter text/template(定義模板) text/template/parse time time(時(shí)間戳) unicode unicode unicode/utf16 unicode/utf8 unsafe unsafe
characters

  • import "go/ast"

  • 概述

  • 索引

  • 示例

概述

ast包聲明 了用于表示 Go 包的語(yǔ)法樹的類型。

索引

  • 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 文件修剪為適當(dāng)?shù)?Go 源文件,以便只保留導(dǎo)出的節(jié)點(diǎn):刪除所有未導(dǎo)出的頂級(jí)標(biāo)識(shí)符及其關(guān)聯(lián)信息(如類型,初始值或函數(shù)體)。非導(dǎo)出字段和導(dǎo)出類型的方法被刪除。File.Comments 列表不會(huì)更改。

FileExports 報(bào)告是否有導(dǎo)出聲明。

func FilterDecl

func FilterDecl(decl Decl, f Filter) bool

FilterDecl 通過(guò)刪除不通過(guò)過(guò)濾器f的所有名稱(包括struct 字段和接口方法名稱,但不包括參數(shù)列表)來(lái)修飾 AST 的 Go 聲明。

FilterDecl 報(bào)告過(guò)濾后是否有任何聲明的名稱。

func FilterFile

func FilterFile(src *File, f Filter) bool

FilterFile 通過(guò)刪除不通過(guò)過(guò)濾器f的頂級(jí)聲明(包括 struct字段和接口方法名稱,但不包含參數(shù)列表)中的所有名稱來(lái)修剪 AST 文件。如果后面的聲明為空,則聲明將從 AST 中刪除。導(dǎo)入聲明總是被刪除。File.Comments 列表不會(huì)更改。

FilterFile 報(bào)告過(guò)濾后是否還有任何頂級(jí)聲明。

func FilterPackage

func FilterPackage(pkg *Package, f Filter) bool

FilterPackage 通過(guò)刪除不通過(guò)過(guò)濾器f的頂級(jí)聲明(包括struct 字段和接口方法名稱,但不包括參數(shù)列表)中的所有名稱來(lái)修剪適用于 Go 包的 AST。如果后面的聲明為空,則聲明將從 AST 中刪除。pkg.Files 列表未更改,因此文件名和頂級(jí)包注釋不會(huì)丟失。

FilterPackage 報(bào)告過(guò)濾后是否有任何頂級(jí)聲明。

func Fprint

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

Fprint 將從 AST 節(jié)點(diǎn)x開始的(sub-)樹打印到 w。如果 fset!= nil,則相對(duì)于該文件集解釋位置信息。否則,位置將被打印為整數(shù)值(文件集特定的偏移量)。

可以提供非零 FieldFilter f 來(lái)控制輸出:打印f(fieldname,fieldvalue)為 true 的 struct 字段;所有其他的都從輸出中過(guò)濾掉。未導(dǎo)出的結(jié)構(gòu)字段從不打印。

func Inspect

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

檢查以深度優(yōu)先順序遍歷AST:它通過(guò)調(diào)用f(node) 開始;節(jié)點(diǎn)不能為零。如果 f 返回 true,Inspect 會(huì)為節(jié)點(diǎn)的每個(gè)非零子節(jié)點(diǎn)遞歸調(diào)用f,然后調(diào)用 f(nil)。

示例

這個(gè)例子演示了如何檢查 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
`// 通過(guò)解析src來(lái)創(chuàng)建AST。
	fset := token.NewFileSet() // 職位相對(duì)于fset
	f, err := parser.ParseFile(fset, "src.go", src, 0)if err != nil {panic(err)}// 檢查AST并打印所有標(biāo)識(shí)符和文字。
	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 報(bào)告名稱是否為導(dǎo)出的 Go 符號(hào)(即,它是否以大寫字母開頭)。

func NotNilFilter

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

NotNilFilter 對(duì)不為零的字段值返回 true;否則返回 false。

func PackageExports

func PackageExports(pkg *Package) bool

PackageExports 將適用于 Go 包的 AST 修剪為僅保留導(dǎo)出的節(jié)點(diǎn)。pkg.Files 列表未更改,因此文件名和頂級(jí)包注釋不會(huì)丟失。

PackageExports 報(bào)告是否有導(dǎo)出聲明;否則返回 false。

func Print

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

Print打印x到標(biāo)準(zhǔn)輸出,跳過(guò)零字段。打?。╢set,x)與Fprint(os.Stdout,fset,x,NotNilFilter)相同。

示例

這個(gè)例子顯示了打印進(jìn)行調(diào)試時(shí) AST 的外觀。

package mainimport ("go/ast""go/parser""go/token")func main() {// src是我們要打印AST的輸入。
	src := `
package main
func main() {
	println("Hello, World!")
}
`// 通過(guò)解析src來(lái)創(chuàng)建AST。
	fset := token.NewFileSet() // 職位相對(duì)于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 對(duì)f中的導(dǎo)入塊中的連續(xù)導(dǎo)入行進(jìn)行排序。它還可以在不丟失數(shù)據(jù)的情況下刪除重復(fù)導(dǎo)入。

func Walk

func Walk(v Visitor, node Node)

以深度優(yōu)先順序遍歷 AST:它通過(guò)調(diào)用 v.Visit(節(jié)點(diǎn))開始; 節(jié)點(diǎn)不能為零。如果由 v.Visit(節(jié)點(diǎn))返回的訪問(wèn)者 w 不為零,則對(duì)于節(jié)點(diǎn)的每個(gè)非零子節(jié)點(diǎn),訪問(wèn)者 w 遞歸地調(diào)用 Walk,隨后調(diào)用 w.Visit(nil)。

type ArrayType

ArrayType 節(jié)點(diǎn)表示一個(gè)數(shù)組或切片類型。

type ArrayType struct {
        Lbrack token.Pos // “[”的位置
        Len    Expr      // [...] T數(shù)組類型的省略號(hào)節(jié)點(diǎn),切片類型為零
        Elt    Expr      // 元素類型}

func (*ArrayType) End

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

func (*ArrayType) Pos

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

type AssignStmt

AssignStmt 節(jié)點(diǎn)表示賦值或短變量聲明。

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 節(jié)點(diǎn)是包含語(yǔ)法錯(cuò)誤的聲明的占位符,其中不能創(chuàng)建正確的聲明節(jié)點(diǎn)。

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 節(jié)點(diǎn)是包含語(yǔ)法錯(cuò)誤的表達(dá)式的占位符,不能創(chuàng)建正確的表達(dá)式節(jié)點(diǎn)。

type BadExpr struct {
        From, To token.Pos // 不良表達(dá)的位置范圍}

func (*BadExpr) End

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

func (*BadExpr) Pos

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

type BadStmt

BadStmt 節(jié)點(diǎn)是包含語(yǔ)法錯(cuò)誤的語(yǔ)句的占位符,其中不能創(chuàng)建正確的語(yǔ)句節(jié)點(diǎn)。

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 節(jié)點(diǎn)表示基本類型的文字。

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 節(jié)點(diǎn)表示一個(gè)二進(jìn)制表達(dá)式。

type BinaryExpr struct {
        X     Expr        // 左操作數(shù)
        OpPos token.Pos   // Op的位置
        Op    token.Token // 操作者
        Y     Expr        // 右操作數(shù)}

func (*BinaryExpr) End

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

func (*BinaryExpr) Pos

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

type BlockStmt

BlockStmt 節(jié)點(diǎn)表示一個(gè)支撐語(yǔ)句列表。

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 節(jié)點(diǎn)表示中斷,繼續(xù),轉(zhuǎn)到或直通語(yǔ)句。

type BranchStmt struct {
        TokPos token.Pos   // Tok的位置
        Tok    token.Token // 關(guān)鍵字令牌(BREAK,CONTINUE,GOTO,F(xiàn)ALLTHROUGH)
        Label  *Ident      // 標(biāo)簽名稱; 或者nil}

func (*BranchStmt) End

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

func (*BranchStmt) Pos

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

type CallExpr

CallExpr 節(jié)點(diǎn)代表一個(gè)表達(dá)式,后跟一個(gè)參數(shù)列表。

type CallExpr struct {
        Fun      Expr      // 函數(shù)表達(dá)
        Lparen   token.Pos // “(”的位置
        Args     []Expr    // 函數(shù)參數(shù); 或者沒(méi)有
        Ellipsis token.Pos // “......”的位置(如果沒(méi)有“......”則為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表示表達(dá)式或類型切換語(yǔ)句的一種情況。

type CaseClause struct {
        Case  token.Pos // “case”或“default”關(guān)鍵字的位置
        List  []Expr    // 表達(dá)式或類型列表; nil表示默認(rèn)情況
        Colon token.Pos // “:”的位置
        Body  []Stmt    // 聲明清單; 或者沒(méi)有}

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 節(jié)點(diǎn)代表一個(gè)通道類型。

type ChanType struct {
        Begin token.Pos // “chan”關(guān)鍵字或“< - ”的位置(以先到者為準(zhǔn))
        Arrow token.Pos // “< - ”的位置(如果沒(méi)有“< - ”則為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 節(jié)點(diǎn)表示選擇語(yǔ)句的情況。

type CommClause struct {
        Case  token.Pos // “case”或“default”關(guān)鍵字的位置
        Comm  Stmt      // 發(fā)送或接收聲明; nil表示默認(rèn)情況
        Colon token.Pos // “:”的位置
        Body  []Stmt    // 聲明清單; 或者沒(méi)有}

func (*CommClause) End

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

func (*CommClause) Pos

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

type Comment

Comment 節(jié)點(diǎn)表示一個(gè) //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 表示一系列注釋,沒(méi)有其他標(biāo)記,也沒(méi)有空行。

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

文本返回注釋的文本。注釋標(biāo)記(//,/* 和 */),行注釋的第一個(gè)空格,以及前導(dǎo)空行和尾行空行被刪除。多個(gè)空行減少為一行,行尾的空格被修剪。除非結(jié)果是空的,否則它是換行符終止的。

type CommentMap

CommentMap 將 AST 節(jié)點(diǎn)映射到與其關(guān)聯(lián)的評(píng)論組列表。請(qǐng)參閱 NewCommentMap 以獲取關(guān)聯(lián)的描述。

type CommentMap map[Node][]*CommentGroup

示例

這個(gè)例子說(shuō)明了如何使用 ast.CommentMap 去除 Go 程序中的變量聲明,同時(shí)保持正確的評(píng)論關(guān)聯(lián)。

package mainimport ("bytes""fmt""go/ast""go/format""go/parser""go/token")func main() {// src是我們創(chuàng)建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
}
`// 通過(guò)解析src來(lái)創(chuàng)建AST。
	fset := token.NewFileSet() // 職位相對(duì)于fset
	f, err := parser.ParseFile(fset, "src.go", src, parser.ParseComments)if err != nil {panic(err)}// 從ast.File的注釋中創(chuàng)建一個(gè)ast.CommentMap。// 這有助于保持注釋之間的關(guān)聯(lián)// 和AST節(jié)點(diǎn)。
	cmap := ast.NewCommentMap(fset, f, f.Comments)// 從聲明列表中刪除第一個(gè)變量聲明。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]}}// 使用注釋地圖過(guò)濾不再屬于的注釋// (與變量聲明相關(guān)的注釋),并創(chuàng)建// 新注釋列表。
	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 通過(guò)將注釋列表的注釋組與節(jié)點(diǎn)指定的 AST 的節(jié)點(diǎn)關(guān)聯(lián)來(lái)創(chuàng)建新的注釋圖。

如果滿足以下條件,則注釋組 g 與節(jié)點(diǎn) n 相關(guān)聯(lián):

 -  g與n結(jié)束時(shí)在同一行開始-  g緊跟在n之后的行上開始,并且有
  g之后和下一個(gè)節(jié)點(diǎn)之前至少有一個(gè)空行-  g在n之前開始,并且不與n之前的節(jié)點(diǎn)相關(guān)聯(lián)
  通過(guò)以前的規(guī)則

NewCommentMap 嘗試將注釋組關(guān)聯(lián)到可能的“l(fā)argest”節(jié)點(diǎn):例如,如果注釋是一個(gè)跟隨分配的行注釋,則注釋與整個(gè)分配相關(guān)聯(lián),而不僅僅是分配中的最后一個(gè)操作數(shù)。

func (CommentMap) Comments

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

評(píng)論返回評(píng)論地圖中評(píng)論組的列表。結(jié)果按來(lái)源順序排序。

func (CommentMap) Filter

func (cmap CommentMap) Filter(node Node) CommentMap

篩選器返回一個(gè)新的注釋映射,其中只包含由節(jié)點(diǎn)指定的 AST 中存在相應(yīng)節(jié)點(diǎn)的 cmap 的條目。

func (CommentMap) String

func (cmap CommentMap) String() string

func (CommentMap) Update

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

更新用新節(jié)點(diǎn)替換評(píng)論映射中的舊節(jié)點(diǎn)并返回新節(jié)點(diǎn)。與舊節(jié)點(diǎn)相關(guān)聯(lián)的注釋與新節(jié)點(diǎn)相關(guān)聯(lián)。

type CompositeLit

CompositeLit 節(jié)點(diǎn)表示復(fù)合文字。

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

func (*CompositeLit) End

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

func (*CompositeLit) Pos

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

type Decl

所有聲明節(jié)點(diǎn)都實(shí)現(xiàn) Decl 接口。

type Decl interface {
        Node        // 包含過(guò)濾或未導(dǎo)出的方法}

type DeclStmt

DeclStmt節(jié)點(diǎn)表示聲明列表中的聲明。

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 節(jié)點(diǎn)表示推遲語(yǔ)句。

type DeferStmt struct {
        Defer token.Pos // “defer”關(guān)鍵字的位置
        Call  *CallExpr}

func (*DeferStmt) End

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

func (*DeferStmt) Pos

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

type Ellipsis

省略號(hào)節(jié)點(diǎn)代表參數(shù)列表中的“...”類型或數(shù)組類型中的“...”長(zhǎng)度。

type Ellipsis struct {
        Ellipsis token.Pos // “...”的位置
        Elt      Expr      // 省略號(hào)元素類型(僅限參數(shù)列表); 或者沒(méi)有

func (*Ellipsis) End

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

func (*Ellipsis) Pos

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

type EmptyStmt

EmptyStmt 節(jié)點(diǎn)表示一個(gè)空語(yǔ)句??照Z(yǔ)句的“position”是緊隨其后的(顯式或隱式)分號(hào)的位置。

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

func (*EmptyStmt) End

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

func (*EmptyStmt) Pos

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

type Expr

所有表達(dá)式節(jié)點(diǎn)都實(shí)現(xiàn) Expr 接口。

type Expr interface {
        Node        // 包含過(guò)濾或未導(dǎo)出的方法}

type ExprStmt

ExprStmt節(jié)點(diǎn)表示語(yǔ)句列表中的(stand-alone)表達(dá)式。

type ExprStmt struct {
        X Expr // 表達(dá)式}

func (*ExprStmt) End

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

func (*ExprStmt) Pos

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

type Field

字段表示結(jié)構(gòu)類型中的字段聲明列表,接口類型中的方法列表或簽名中的參數(shù)/結(jié)果聲明。

type Field struct {
        Doc     *CommentGroup // 相關(guān)文件; 或者沒(méi)有
        Names   []*Ident      // 字段/方法/參數(shù)名稱;或者如果匿名字段為零
        Type    Expr          // 字段/方法/參數(shù)類型
        Tag     *BasicLit     // 字段標(biāo)簽; 或者沒(méi)有
        Comment *CommentGroup // 行注釋; 或者沒(méi)有}

func (*Field) End

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

func (*Field) Pos

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

type FieldFilter

FieldFilter可以提供給Fprint來(lái)控制輸出。

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

type FieldList

FieldList表示一個(gè)由圓括號(hào)或大括號(hào)包圍的字段列表。

type FieldList struct {
        Opening token.Pos // 開括號(hào)/括號(hào)的位置,如果有的話
        List    []*Field  // 字段列表; 或者沒(méi)有
        Closing token.Pos // 右括號(hào)/括號(hào)的位置,如果有的話}

func (*FieldList) End

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

func (*FieldList) NumFields

func (f *FieldList) NumFields() int

NumFields 返回 FieldList 中的(命名和匿名字段)的數(shù)量。

func (*FieldList) Pos

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

type File

一個(gè)文件節(jié)點(diǎn)代表一個(gè) Go 源文件。

注釋列表按照外觀順序包含源文件中的所有注釋,包括通過(guò) Doc 和注釋字段從其他節(jié)點(diǎn)指向的注釋。

為了正確打印包含注釋的源代碼(使用軟件包 go/format 和 go/printer),必須特別小心在修改文件語(yǔ)法樹時(shí)更新注釋:對(duì)于打印,注釋根據(jù)其位置散布在令牌之間。如果語(yǔ)法樹節(jié)點(diǎn)被刪除或移動(dòng),則其附近的相關(guān)注釋也必須從 File.Comments 列表中移除或相應(yīng)移動(dòng)(通過(guò)更新其位置)。一個(gè) CommentMap 可以用來(lái)方便這些操作。

注釋是否以及如何與節(jié)點(diǎn)相關(guān)取決于操作程序?qū)φZ(yǔ)法樹的解釋:除了與節(jié)點(diǎn)直接相關(guān)的文檔和評(píng)論評(píng)論之外,其余注釋是“自由浮動(dòng)的(free-floating)”(另見(jiàn)issues #18593, #20744)。

type File struct {
        Doc        *CommentGroup   // 相關(guān)文件; 或者沒(méi)有
        Package    token.Pos       // “package”關(guān)鍵字的位置
        Name       *Ident          // 包名稱
        Decls      []Decl          // 頂級(jí)聲明; 或者沒(méi)有
        Scope      *Scope          // 包范圍(僅限此文件)
        Imports    []*ImportSpec   // 在此文件中導(dǎo)入
        Unresolved []*Ident        // 此文件中未解析的標(biāo)識(shí)符
        Comments   []*CommentGroup // 源文件中所有注釋的列表}

func MergePackageFiles

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

MergePackageFiles 通過(guò)合并屬于包的文件的 AST 來(lái)創(chuàng)建文件 AST。模式標(biāo)志控制合并行為。

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 語(yǔ)句。

type ForStmt struct {
        For  token.Pos // “for”關(guān)鍵字的位置
        Init Stmt      // 初始化語(yǔ)句; 或者沒(méi)有
        Cond Expr      // 條件; 或者沒(méi)有
        Post Stmt      // post iteration語(yǔ)句; 或者沒(méi)有
        Body *BlockStmt}

func (*ForStmt) End

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

func (*ForStmt) Pos

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

type FuncDecl

FuncDecl 節(jié)點(diǎn)表示一個(gè)函數(shù)聲明。

type FuncDecl struct {
        Doc  *CommentGroup // 相關(guān)文件; 或者沒(méi)有
        Recv *FieldList    // 接收器(方法); 或零(函數(shù))
        Name *Ident        // 函數(shù)/方法名稱
        Type *FuncType     // 函數(shù)簽名:“func”關(guān)鍵字的參數(shù),結(jié)果和位置
        Body *BlockStmt    // 功能體; 或者對(duì)于外部(非Go)功能為零

func (*FuncDecl) End

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

func (*FuncDecl) Pos

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

type FuncLit

一個(gè)FuncLit節(jié)點(diǎn)代表一個(gè)函數(shù)文字。

type FuncLit struct {
        Type *FuncType  // 函數(shù)類型
        Body *BlockStmt // 函數(shù)體}

func (*FuncLit) End

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

func (*FuncLit) Pos

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

type FuncType

FuncType 節(jié)點(diǎn)表示一個(gè)函數(shù)類型。

type FuncType struct {
        Func    token.Pos  // “func”關(guān)鍵字的位置(如果沒(méi)有“func”,則為token.NoPos)
        Params  *FieldList // (傳入)參數(shù);非空
        Results *FieldList // (外向)結(jié)果;或者沒(méi)有}

func (*FuncType) End

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

func (*FuncType) Pos

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

type GenDecl

GenDecl節(jié)點(diǎn)(通用聲明節(jié)點(diǎn))表示一個(gè)導(dǎo)入,常量,類型或變量聲明。有效的 Lparen 位置(Lparen.IsValid())表示括號(hào)聲明。

Tok 值與 Specs 元素類型之間的關(guān)系:

token.IMPORT  *ImportSpec
token.CONST   *ValueSpec
token.TYPE    *TypeSpec
token.VAR     *ValueSpec
type GenDecl struct {
        Doc    *CommentGroup // 相關(guān)文件; 或者沒(méi)有
        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 節(jié)點(diǎn)表示 go 語(yǔ)句。

type GoStmt struct {
        Go   token.Pos // “go” 關(guān)鍵字的位置
        Call *CallExpr}

func (*GoStmt) End

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

func (*GoStmt) Pos

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

type Ident

一個(gè) Ident 節(jié)點(diǎn)表示一個(gè)標(biāo)識(shí)符。

type Ident struct {
        NamePos token.Pos // 確定位置
        Name    string    // 標(biāo)識(shí)符名稱
        Obj     *Object   // 表示對(duì)象; 或者沒(méi)有}

func NewIdent

func NewIdent(name string) *Ident

NewIdent 創(chuàng)建一個(gè)沒(méi)有位置的新的 Ident。對(duì)于由 Go 解析器以外的代碼生成的 AST 非常有用。

func (*Ident) End

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

func (*Ident) IsExported

func (id *Ident) IsExported() bool

IsExported 報(bào)告 id 是否是導(dǎo)出的 Go 符號(hào)(即,它是否以大寫字母開頭)。

func (*Ident) Pos

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

func (*Ident) String

func (id *Ident) String() string

type IfStmt

IfStmt 節(jié)點(diǎn)表示一個(gè) if 語(yǔ)句。

type IfStmt struct {
        If   token.Pos // “if”關(guān)鍵字的位置
        Init Stmt      // 初始化語(yǔ)句; 或者沒(méi)有
        Cond Expr      // 條件
        Body *BlockStmt
        Else Stmt // 別的分支; 或者沒(méi)有}

func (*IfStmt) End

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

func (*IfStmt) Pos

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

type ImportSpec

ImportSpec 節(jié)點(diǎn)表示單個(gè)包導(dǎo)入。

type ImportSpec struct {
        Doc     *CommentGroup // 相關(guān)文件; 或者沒(méi)有
        Name    *Ident        // 本地包名(包括“.”); 或者沒(méi)有
        Path    *BasicLit     // 導(dǎo)入路徑
        Comment *CommentGroup // 行注釋;或者沒(méi)有
        EndPos  token.Pos     // 規(guī)范結(jié)束(如果非零則覆蓋Path.Pos)}

func (*ImportSpec) End

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

func (*ImportSpec) Pos

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

type Importer

導(dǎo)入程序解析導(dǎo)入路徑以打包對(duì)象。導(dǎo)入映射記錄已導(dǎo)入的包,并按包ID(規(guī)范導(dǎo)入路徑)進(jìn)行索引。導(dǎo)入程序必須確定規(guī)范導(dǎo)入路徑并檢查導(dǎo)圖,看它是否已經(jīng)存在于導(dǎo)入導(dǎo)圖中。如果是這樣,導(dǎo)入器可以返回地圖條目。否則,導(dǎo)入程序應(yīng)將給定路徑的包數(shù)據(jù)加載到新的 *Object(pkg)中,在導(dǎo)入映射中記錄 pkg,然后返回 pkg。

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

type IncDecStmt

IncDecStmt 節(jié)點(diǎn)表示增量或減量語(yǔ)句。

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 節(jié)點(diǎn)表示后跟一個(gè)索引的表達(dá)式。

type IndexExpr struct {
        X      Expr      // 表達(dá)式
        Lbrack token.Pos // “[”的位置
        Index  Expr      // 索引表達(dá)式
        Rbrack token.Pos // “]的位置}

func (*IndexExpr) End

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

func (*IndexExpr) Pos

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

type InterfaceType

InterfaceType 節(jié)點(diǎn)表示接口類型。

type InterfaceType struct {
        Interface  token.Pos  // “interface”關(guān)鍵字的位置
        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節(jié)點(diǎn)表示復(fù)合文字中的(鍵:值)對(duì)。

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 節(jié)點(diǎn)表示帶標(biāo)簽的語(yǔ)句。

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節(jié)點(diǎn)表示地圖類型。

type MapType struct {
        Map   token.Pos // “map”關(guān)鍵字的位置
        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 標(biāo)志控制 MergePackageFiles 的行為。

type MergeMode uint
const (        // 如果設(shè)置,則排除重復(fù)的函數(shù)聲明。
        FilterFuncDuplicates MergeMode = 1 << iota        // 如果設(shè)置,則與特定的注釋無(wú)關(guān)        // AST節(jié)點(diǎn)(作為Doc或Comment)被排除在外。
        FilterUnassociatedComments        // 如果設(shè)置,則排除重復(fù)的導(dǎo)入聲明。
        FilterImportDuplicates)

type Node

所有節(jié)點(diǎn)類型都實(shí)現(xiàn) Node 接口。

type Node interface {        Pos() token.Pos // 屬于該節(jié)點(diǎn)的第一個(gè)字符的位置        End() token.Pos // 緊接在節(jié)點(diǎn)之后的第一個(gè)字符的位置}

type ObjKind

ObjKind 描述了一個(gè)對(duì)象代表什么。

type ObjKind int

可能的對(duì)象種類的列表。

const (
        Bad ObjKind = iota // 用于錯(cuò)誤處理
        Pkg                // 包
        Con                // 常量
        Typ                // 類型
        Var                // 變量
        Fun                // 函數(shù)或方法
        Lbl                // 標(biāo)簽)

func (ObjKind) String

func (kind ObjKind) String() string

type Object

一個(gè)對(duì)象描述了一個(gè)命名的語(yǔ)言實(shí)體,如包,常量,類型,變量,函數(shù)(包括方法)或標(biāo)簽。

數(shù)據(jù)字段包含對(duì)象特定的數(shù)據(jù):

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{} // 對(duì)應(yīng)的Field,XxxSpec,F(xiàn)uncDecl,LabeledStmt,AssignStmt,Scope; 或者沒(méi)有
        Data interface{} // 特定對(duì)象數(shù)據(jù);或者沒(méi)有
        Type interface{} // 占位符的類型信息; 可能是零}

func NewObj

func NewObj(kind ObjKind, name string) *Object

NewObj 創(chuàng)建一個(gè)給定種類和名稱的新對(duì)象。

func (*Object) Pos

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

Pos 計(jì)算對(duì)象名稱聲明的源位置。如果無(wú)法計(jì)算結(jié)果可能是無(wú)效位置(obj.Decl 可能為零或不正確)。

type Package

Package 節(jié)點(diǎn)表示一組源文件,共同構(gòu)建 Go 包。

type Package struct {
        Name    string             // 包名稱
        Scope   *Scope             // 所有文件的包范圍
        Imports map[string]*Object // 包id的地圖 - >包對(duì)象
        Files   map[string]*File   // 按文件名轉(zhuǎn)到源文件

func NewPackage

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

NewPackage 從一組 File 節(jié)點(diǎn)創(chuàng)建一個(gè)新的 Package 節(jié)點(diǎn)。它解決跨文件的未解析標(biāo)識(shí)符,并相應(yīng)地更新每個(gè)文件的未解析列表。如果提供了非零導(dǎo)入器和 Universe 范圍,則它們將用于解析未在任何包文件中聲明的標(biāo)識(shí)符。任何剩余的未解析標(biāo)識(shí)符都會(huì)報(bào)告為未聲明。如果這些文件屬于不同的軟件包,則會(huì)選擇一個(gè)軟件包名稱,并報(bào)告具有不同軟件包名稱的文件,然后將其忽略。結(jié)果是包節(jié)點(diǎn)和 scanner.ErrorList 是否有錯(cuò)誤。

func (*Package) End

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

func (*Package) Pos

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

type ParenExpr

一個(gè) ParenExpr 節(jié)點(diǎn)表示一個(gè)帶括號(hào)的表達(dá)式。

type ParenExpr struct {
        Lparen token.Pos // “(”的位置
        X      Expr      // 括號(hào)表達(dá)式
        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 語(yǔ)句。

type RangeStmt struct {
        For        token.Pos   // “for”關(guān)鍵字的位置
        Key, Value Expr        // 關(guān)鍵,值可能是零
        TokPos     token.Pos   // Tok的位置; 如果Key == nil則無(wú)效
        Tok        token.Token // 如果Key == nil,ASSIGN,DEFINE,則為ILLEGAL
        X          Expr        // 值范圍超過(guò)
        Body       *BlockStmt}

func (*RangeStmt) End

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

func (*RangeStmt) Pos

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

type ReturnStmt

ReturnStmt 節(jié)點(diǎn)表示一個(gè)返回語(yǔ)句。

type ReturnStmt struct {
        Return  token.Pos // “return”關(guān)鍵字的位置
        Results []Expr    // 結(jié)果表達(dá)式;或者沒(méi)有}

func (*ReturnStmt) End

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

func (*ReturnStmt) Pos

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

type Scope

Scope 維護(hù)在范圍中聲明的一組指定語(yǔ)言實(shí)體以及到緊鄰的(外部)范圍的鏈接。

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

func NewScope

func NewScope(outer *Scope) *Scope

NewScope 創(chuàng)建一個(gè)嵌套在外部作用域中的新作用域。

func (*Scope) Insert

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

Insert 嘗試將命名對(duì)象 obj 插入到作用域中。如果范圍已經(jīng)包含名稱相同的對(duì)象 alt,則 Insert 會(huì)保持范圍不變并返回 alt。否則,它插入 obj 并返回 nil。

func (*Scope) Lookup

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

如果在范圍s中找到,Lookup 將返回具有給定名稱的對(duì)象,否則返回 nil。外部作用域被忽略。

func (*Scope) String

func (s *Scope) String() string

調(diào)試支持

type SelectStmt

SelectStmt 節(jié)點(diǎn)表示一個(gè) select 語(yǔ)句。

type SelectStmt struct {
        Select token.Pos  // “select”關(guān)鍵字的位置
        Body   *BlockStmt // 僅限CommClauses}

func (*SelectStmt) End

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

func (*SelectStmt) Pos

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

type SelectorExpr

SelectorExpr 節(jié)點(diǎn)表示一個(gè)表達(dá)式,后跟一個(gè)選擇器。

type SelectorExpr struct {
        X   Expr   // 表達(dá)式
        Sel *Ident // 場(chǎng)選擇器}

func (*SelectorExpr) End

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

func (*SelectorExpr) Pos

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

type SendStmt

SendStmt 節(jié)點(diǎn)表示發(fā)送語(yǔ)句。

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 節(jié)點(diǎn)表示一個(gè)表達(dá)式,后跟切片索引。

type SliceExpr struct {
        X      Expr      // 表達(dá)式
        Lbrack token.Pos // “[”的位置
        Low    Expr      // 切片范圍的開始;或者沒(méi)有
        High   Expr      // 切片范圍的結(jié)束; 或者沒(méi)有
        Max    Expr      // 最大切片容量; 或者沒(méi)有
        Slice3 bool      // 如果3-index切片(存在2個(gè)冒號(hào)),則為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 中的任何一個(gè)。

type Spec interface {
        Node        // 包含過(guò)濾或未導(dǎo)出的方法}

type StarExpr

StarExpr 節(jié)點(diǎn)表示形式為“*”表達(dá)式的表達(dá)式。在語(yǔ)義上它可以是一個(gè)一元“*”表達(dá)式或指針類型。

type StarExpr struct {
        Star token.Pos // “*”的位置
        X    Expr      // 操作數(shù)}

func (*StarExpr) End

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

func (*StarExpr) Pos

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

type Stmt

所有語(yǔ)句節(jié)點(diǎn)都實(shí)現(xiàn) Stmt 接口。

type Stmt interface {
        Node        // 包含過(guò)濾或未導(dǎo)出的方法}

type StructType

StructType 節(jié)點(diǎn)表示一個(gè)結(jié)構(gòu)類型。

type StructType struct {
        Struct     token.Pos  // “struct”關(guān)鍵字的位置
        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

一個(gè) SwitchStmt 節(jié)點(diǎn)表示一個(gè)表達(dá)式 switch 語(yǔ)句。

type SwitchStmt struct {
        Switch token.Pos  // “switch”關(guān)鍵字的位置
        Init   Stmt       // 初始化語(yǔ)句; 或者沒(méi)有
        Tag    Expr       // 標(biāo)簽表達(dá); 或者沒(méi)有
        Body   *BlockStmt // 僅限CaseClauses}

func (*SwitchStmt) End

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

func (*SwitchStmt) Pos

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

type TypeAssertExpr

TypeAssertExpr 節(jié)點(diǎn)表示一個(gè)表達(dá)式,后跟一個(gè)類型斷言。

type TypeAssertExpr struct {
        X      Expr      // 表達(dá)式
        Lparen token.Pos // “(”的位置
        Type   Expr      // 斷言類型; nil表示類型開關(guān)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 節(jié)點(diǎn)代表一個(gè)類型聲明(TypeSpec 生成)。

type TypeSpec struct {
        Doc     *CommentGroup // 相關(guān)文件;或者沒(méi)有
        Name    *Ident        // 類型名稱
        Assign  token.Pos     // '='的位置,如果有的話
        Type    Expr          // *Ident,*ParenExpr,*SelectorExpr,*StarExpr或任何*XxxTypes
        Comment *CommentGroup // 行注釋;或者沒(méi)有}

func (*TypeSpec) End

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

func (*TypeSpec) Pos

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

type TypeSwitchStmt

TypeSwitchStmt 節(jié)點(diǎn)表示類型切換語(yǔ)句。

type TypeSwitchStmt struct {
        Switch token.Pos  // “switch”關(guān)鍵字的位置
        Init   Stmt       // 初始化語(yǔ)句; 或者沒(méi)有
        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 節(jié)點(diǎn)表示一個(gè)一元表達(dá)式。一元“*”表達(dá)式通過(guò) StarExpr 節(jié)點(diǎn)表示。

type UnaryExpr struct {
        OpPos token.Pos   // Op 的位置
        Op    token.Token // 操作者
        X     Expr        // 操作數(shù)}

func (*UnaryExpr) End

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

func (*UnaryExpr) Pos

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

type ValueSpec

ValueSpec 節(jié)點(diǎn)表示常量或變量聲明(ConstSpec 或 VarSpec 生成)。

type ValueSpec struct {
        Doc     *CommentGroup // 相關(guān)文件; 或者沒(méi)有
        Names   []*Ident      // 值名稱(len(Names) > 0)
        Type    Expr          // 值類型; 或者沒(méi)有
        Values  []Expr        // 初始值; 或者沒(méi)有
        Comment *CommentGroup // 行注釋 或者沒(méi)有}

func (*ValueSpec) End

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

func (*ValueSpec) Pos

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

type Visitor

為 Walk 遇到的每個(gè)節(jié)點(diǎn)調(diào)用訪問(wèn)者訪問(wèn)方法。如果結(jié)果訪問(wèn)者w不為零,則 Walk 訪問(wèn)w的每個(gè)子節(jié)點(diǎn),然后調(diào)用 w.Visit(nil)。

type Visitor interface {        Visit(node Node) (w Visitor)}
Previous article: Next article: