package ast

Import Path
	go/ast (on golang.org and go.dev)

Dependency Relation
	imports 10 packages, and imported by 8 packages

Involved Source Files Package ast declares the types used to represent syntax trees for Go packages. ast_notypeparams.go commentmap.go filter.go import.go print.go resolve.go scope.go walk.go walk_notypeparams.go
Code Examples package main import ( "bytes" "fmt" "go/ast" "go/format" "go/parser" "go/token" ) func main() { // src is the input for which we create the AST that we // are going to manipulate. 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 } ` // Create the AST by parsing src. fset := token.NewFileSet() // positions are relative to fset f, err := parser.ParseFile(fset, "src.go", src, parser.ParseComments) if err != nil { panic(err) } // Create an ast.CommentMap from the ast.File's comments. // This helps keeping the association between comments // and AST nodes. cmap := ast.NewCommentMap(fset, f, f.Comments) // Remove the first variable declaration from the list of declarations. 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] break } } // Use the comment map to filter comments that don't belong anymore // (the comments associated with the variable declaration), and create // the new comments list. f.Comments = cmap.Filter(f).Comments() // Print the modified AST. var buf bytes.Buffer if err := format.Node(&buf, fset, f); err != nil { panic(err) } fmt.Printf("%s", buf.Bytes()) } package main import ( "fmt" "go/ast" "go/parser" "go/token" ) func main() { // src is the input for which we want to inspect the AST. src := ` package p const c = 1.0 var X = f(3.14)*2 + c ` // Create the AST by parsing src. fset := token.NewFileSet() // positions are relative to fset f, err := parser.ParseFile(fset, "src.go", src, 0) if err != nil { panic(err) } // Inspect the AST and print all identifiers and literals. ast.Inspect(f, func(n ast.Node) bool { var s string switch x := n.(type) { case *ast.BasicLit: s = x.Value case *ast.Ident: s = x.Name } if s != "" { fmt.Printf("%s:\t%s\n", fset.Position(n.Pos()), s) } return true }) } package main import ( "go/ast" "go/parser" "go/token" ) func main() { // src is the input for which we want to print the AST. src := ` package main func main() { println("Hello, World!") } ` // Create the AST by parsing src. fset := token.NewFileSet() // positions are relative to fset f, err := parser.ParseFile(fset, "", src, 0) if err != nil { panic(err) } // Print the AST. ast.Print(fset, f) }
Package-Level Type Names (total 70)
/* sort by: | */
An ArrayType node represents an array or slice type. // element type // position of "[" // Ellipsis node for [...]T array types, nil for slice types (*T) End() token.Pos (*T) Pos() token.Pos *T : Expr *T : Node
An AssignStmt node represents an assignment or a short variable declaration. Lhs []Expr Rhs []Expr // assignment token, DEFINE // position of Tok (*T) End() token.Pos (*T) Pos() token.Pos *T : Node *T : Stmt
A BadDecl node is a placeholder for a declaration containing syntax errors for which a correct declaration node cannot be created. // position range of bad expression // position range of bad expression (*T) End() token.Pos (*T) Pos() token.Pos *T : Decl *T : Node
A BadExpr node is a placeholder for an expression containing syntax errors for which a correct expression node cannot be created. // position range of bad expression // position range of bad expression (*T) End() token.Pos (*T) Pos() token.Pos *T : Expr *T : Node
A BadStmt node is a placeholder for statements containing syntax errors for which no correct statement nodes can be created. // position range of bad expression // position range of bad expression (*T) End() token.Pos (*T) Pos() token.Pos *T : Node *T : Stmt
A BasicLit node represents a literal of basic type. // token.INT, token.FLOAT, token.IMAG, token.CHAR, or token.STRING // literal string; e.g. 42, 0x7f, 3.14, 1e-9, 2.4i, 'a', '\x7f', "foo" or `\m\n\o` // literal position (*T) End() token.Pos (*T) Pos() token.Pos *T : Expr *T : Node
A BinaryExpr node represents a binary expression. // operator // position of Op // left operand // right operand (*T) End() token.Pos (*T) Pos() token.Pos *T : Expr *T : Node
A BlockStmt node represents a braced statement list. // position of "{" List []Stmt // position of "}", if any (may be absent due to syntax error) (*T) End() token.Pos (*T) Pos() token.Pos *T : Node *T : Stmt
A BranchStmt node represents a break, continue, goto, or fallthrough statement. // label name; or nil // keyword token (BREAK, CONTINUE, GOTO, FALLTHROUGH) // position of Tok (*T) End() token.Pos (*T) Pos() token.Pos *T : Node *T : Stmt
A CallExpr node represents an expression followed by an argument list. // function arguments; or nil // position of "..." (token.NoPos if there is no "...") // function expression // position of "(" // position of ")" (*T) End() token.Pos (*T) Pos() token.Pos *T : Expr *T : Node
A CaseClause represents a case of an expression or type switch statement. // statement list; or nil // position of "case" or "default" keyword // position of ":" // list of expressions or types; nil means default case (*T) End() token.Pos (*T) Pos() token.Pos *T : Node *T : Stmt
The direction of a channel type is indicated by a bit mask including one or both of the following constants. const RECV const SEND
A ChanType node represents a channel type. // position of "<-" (token.NoPos if there is no "<-") // position of "chan" keyword or "<-" (whichever comes first) // channel direction // value type (*T) End() token.Pos (*T) Pos() token.Pos *T : Expr *T : Node
A CommClause node represents a case of a select statement. // statement list; or nil // position of "case" or "default" keyword // position of ":" // send or receive statement; nil means default case (*T) End() token.Pos (*T) Pos() token.Pos *T : Node *T : Stmt
A Comment node represents a single //-style or /*-style comment. The Text field contains the comment text without carriage returns (\r) that may have been present in the source. Because a comment's end position is computed using len(Text), the position reported by End() does not match the true source end position for comments containing carriage returns. // position of "/" starting the comment // comment text (excluding '\n' for //-style comments) (*T) End() token.Pos (*T) Pos() token.Pos *T : Node
A CommentGroup represents a sequence of comments with no other tokens and no empty lines between. // len(List) > 0 (*T) End() token.Pos (*T) Pos() token.Pos Text returns the text of the comment. Comment markers (//, /*, and */), the first space of a line comment, and leading and trailing empty lines are removed. Comment directives like "//line" and "//go:noinline" are also removed. Multiple empty lines are reduced to one, and trailing space on lines is trimmed. Unless the result is empty, it is newline-terminated. *T : Node func CommentMap.Comments() []*CommentGroup func NewCommentMap(fset *token.FileSet, node Node, comments []*CommentGroup) CommentMap
A CommentMap maps an AST node to a list of comment groups associated with it. See NewCommentMap for a description of the association. Comments returns the list of comment groups in the comment map. The result is sorted in source order. Filter returns a new comment map consisting of only those entries of cmap for which a corresponding node exists in the AST specified by node. ( T) String() string Update replaces an old node in the comment map with the new node and returns the new node. Comments that were associated with the old node are associated with the new node. T : expvar.Var T : fmt.Stringer func NewCommentMap(fset *token.FileSet, node Node, comments []*CommentGroup) CommentMap func CommentMap.Filter(node Node) CommentMap
A CompositeLit node represents a composite literal. // list of composite elements; or nil // true if (source) expressions are missing in the Elts list // position of "{" // position of "}" // literal type; or nil (*T) End() token.Pos (*T) Pos() token.Pos *T : Expr *T : Node
All declaration nodes implement the Decl interface. // position of first character immediately after the node // position of first character belonging to the node *BadDecl *FuncDecl *GenDecl T : Node func FilterDecl(decl Decl, f Filter) bool
A DeclStmt node represents a declaration in a statement list. // *GenDecl with CONST, TYPE, or VAR token (*T) End() token.Pos (*T) Pos() token.Pos *T : Node *T : Stmt
A DeferStmt node represents a defer statement. Call *CallExpr // position of "defer" keyword (*T) End() token.Pos (*T) Pos() token.Pos *T : Node *T : Stmt
An Ellipsis node stands for the "..." type in a parameter list or the "..." length in an array type. // position of "..." // ellipsis element type (parameter lists only); or nil (*T) End() token.Pos (*T) Pos() token.Pos *T : Expr *T : Node
An EmptyStmt node represents an empty statement. The "position" of the empty statement is the position of the immediately following (explicit or implicit) semicolon. // if set, ";" was omitted in the source // position of following ";" (*T) End() token.Pos (*T) Pos() token.Pos *T : Node *T : Stmt
All expression nodes implement the Expr interface. // position of first character immediately after the node // position of first character belonging to the node *ArrayType *BadExpr *BasicLit *BinaryExpr *CallExpr *ChanType *CompositeLit *Ellipsis *FuncLit *FuncType *Ident *IndexExpr *InterfaceType *KeyValueExpr *MapType *ParenExpr *SelectorExpr *SliceExpr *StarExpr *StructType *TypeAssertExpr *UnaryExpr T : Node func go/internal/typeparams.PackExpr(list []Expr) Expr func go/internal/typeparams.UnpackExpr(expr Expr) []Expr func go/parser.ParseExpr(x string) (Expr, error) func go/parser.ParseExprFrom(fset *token.FileSet, filename string, src interface{}, mode parser.Mode) (expr Expr, err error) func go/internal/typeparams.PackExpr(list []Expr) Expr func go/internal/typeparams.UnpackExpr(expr Expr) []Expr func go/types.CheckExpr(fset *token.FileSet, pkg *types.Package, pos token.Pos, expr Expr, info *types.Info) (err error) func go/types.ExprString(x Expr) string func go/types.WriteExpr(buf *bytes.Buffer, x Expr) func go/types.(*Info).TypeOf(e Expr) types.Type
An ExprStmt node represents a (stand-alone) expression in a statement list. // expression (*T) End() token.Pos (*T) Pos() token.Pos *T : Node *T : Stmt
A Field represents a Field declaration list in a struct type, a method list in an interface type, or a parameter/result declaration in a signature. Field.Names is nil for unnamed parameters (parameter lists which only contain types) and embedded struct fields. In the latter case, the field name is the type name. Field.Names contains a single name "type" for elements of interface type lists. Types belonging to the same type list share the same "type" identifier which also records the position of that keyword. // line comments; or nil // associated documentation; or nil // field/method/(type) parameter names, or type "type"; or nil // field tag; or nil // field/method/parameter type, type list type; or nil (*T) End() token.Pos (*T) Pos() token.Pos *T : Node
A FieldFilter may be provided to Fprint to control the output. func Fprint(w io.Writer, fset *token.FileSet, x interface{}, f FieldFilter) error
A FieldList represents a list of Fields, enclosed by parentheses or braces. // position of closing parenthesis/brace, if any // field list; or nil // position of opening parenthesis/brace, if any (*T) End() token.Pos NumFields returns the number of parameters or struct fields represented by a FieldList. (*T) Pos() token.Pos *T : Node func go/internal/typeparams.Get(Node) *FieldList func go/internal/typeparams.Set(node Node, params *FieldList)
A File node represents a Go source file. The Comments list contains all comments in the source file in order of appearance, including the comments that are pointed to from other nodes via Doc and Comment fields. For correct printing of source code containing comments (using packages go/format and go/printer), special care must be taken to update comments when a File's syntax tree is modified: For printing, comments are interspersed between tokens based on their position. If syntax tree nodes are removed or moved, relevant comments in their vicinity must also be removed (from the File.Comments list) or moved accordingly (by updating their positions). A CommentMap may be used to facilitate some of these operations. Whether and how a comment is associated with a node depends on the interpretation of the syntax tree by the manipulating program: Except for Doc and Comment comments directly associated with nodes, the remaining comments are "free-floating" (see also issues #18593, #20744). // list of all comments in the source file // top-level declarations; or nil // associated documentation; or nil // imports in this file // package name // position of "package" keyword // package scope (this file only) // unresolved identifiers in this file (*T) End() token.Pos (*T) Pos() token.Pos *T : Node func MergePackageFiles(pkg *Package, mode MergeMode) *File func go/parser.ParseFile(fset *token.FileSet, filename string, src interface{}, mode parser.Mode) (f *File, err error) func FileExports(src *File) bool func FilterFile(src *File, f Filter) bool func NewPackage(fset *token.FileSet, files map[string]*File, importer Importer, universe *Scope) (*Package, error) func SortImports(fset *token.FileSet, f *File) func go/doc.Examples(testFiles ...*File) []*doc.Example func go/doc.NewFromFiles(fset *token.FileSet, files []*File, importPath string, opts ...interface{}) (*doc.Package, error) func go/types.(*Checker).Files(files []*File) error func go/types.(*Config).Check(path string, fset *token.FileSet, files []*File, info *types.Info) (*types.Package, error)
func FilterDecl(decl Decl, f Filter) bool func FilterFile(src *File, f Filter) bool func FilterPackage(pkg *Package, f Filter) bool
A ForStmt represents a for statement. Body *BlockStmt // condition; or nil // position of "for" keyword // initialization statement; or nil // post iteration statement; or nil (*T) End() token.Pos (*T) Pos() token.Pos *T : Node *T : Stmt
A FuncDecl node represents a function declaration. // function body; or nil for external (non-Go) function // associated documentation; or nil // function/method name // receiver (methods); or nil (functions) // function signature: type and value parameters, results, and position of "func" keyword (*T) End() token.Pos (*T) Pos() token.Pos *T : Decl *T : Node
A FuncLit node represents a function literal. // function body // function type (*T) End() token.Pos (*T) Pos() token.Pos *T : Expr *T : Node
A FuncType node represents a function type. // position of "func" keyword (token.NoPos if there is no "func") // (incoming) parameters; non-nil // (outgoing) results; or nil (*T) End() token.Pos (*T) Pos() token.Pos *T : Expr *T : Node
A GenDecl node (generic declaration node) represents an import, constant, type or variable declaration. A valid Lparen position (Lparen.IsValid()) indicates a parenthesized declaration. Relationship between Tok value and Specs element type: token.IMPORT *ImportSpec token.CONST *ValueSpec token.TYPE *TypeSpec token.VAR *ValueSpec // associated documentation; or nil // position of '(', if any // position of ')', if any Specs []Spec // IMPORT, CONST, TYPE, or VAR // position of Tok (*T) End() token.Pos (*T) Pos() token.Pos *T : Decl *T : Node
A GoStmt node represents a go statement. Call *CallExpr // position of "go" keyword (*T) End() token.Pos (*T) Pos() token.Pos *T : Node *T : Stmt
An Ident node represents an identifier. // identifier name // identifier position // denoted object; or nil (*T) End() token.Pos IsExported reports whether id starts with an upper-case letter. (*T) Pos() token.Pos (*T) String() string *T : Expr *T : Node *T : expvar.Var *T : fmt.Stringer func NewIdent(name string) *Ident func go/types.(*Info).ObjectOf(id *Ident) types.Object
An IfStmt node represents an if statement. Body *BlockStmt // condition // else branch; or nil // position of "if" keyword // initialization statement; or nil (*T) End() token.Pos (*T) Pos() token.Pos *T : Node *T : Stmt
An Importer resolves import paths to package Objects. The imports map records the packages already imported, indexed by package id (canonical import path). An Importer must determine the canonical import path and check the map to see if it is already present in the imports map. If so, the Importer can return the map entry. Otherwise, the Importer should load the package data for the given path into a new *Object (pkg), record pkg in the imports map, and then return pkg. func NewPackage(fset *token.FileSet, files map[string]*File, importer Importer, universe *Scope) (*Package, error)
An ImportSpec node represents a single package import. // line comments; or nil // associated documentation; or nil // end of spec (overrides Path.Pos if nonzero) // local package name (including "."); or nil // import path (*T) End() token.Pos (*T) Pos() token.Pos *T : Node *T : Spec
An IncDecStmt node represents an increment or decrement statement. // INC or DEC // position of Tok X Expr (*T) End() token.Pos (*T) Pos() token.Pos *T : Node *T : Stmt
An IndexExpr node represents an expression followed by an index. // index expression // position of "[" // position of "]" // expression (*T) End() token.Pos (*T) Pos() token.Pos *T : Expr *T : Node
An InterfaceType node represents an interface type. // true if (source) methods or types are missing in the Methods list // position of "interface" keyword // list of embedded interfaces, methods, or types (*T) End() token.Pos (*T) Pos() token.Pos *T : Expr *T : Node
A KeyValueExpr node represents (key : value) pairs in composite literals. // position of ":" Key Expr Value Expr (*T) End() token.Pos (*T) Pos() token.Pos *T : Expr *T : Node
A LabeledStmt node represents a labeled statement. // position of ":" Label *Ident Stmt Stmt (*T) End() token.Pos (*T) Pos() token.Pos *T : Node *T : Stmt
A MapType node represents a map type. Key Expr // position of "map" keyword Value Expr (*T) End() token.Pos (*T) Pos() token.Pos *T : Expr *T : Node
The MergeMode flags control the behavior of MergePackageFiles. func MergePackageFiles(pkg *Package, mode MergeMode) *File const FilterFuncDuplicates const FilterImportDuplicates const FilterUnassociatedComments
All node types implement the Node interface. // position of first character immediately after the node // position of first character belonging to the node *ArrayType *AssignStmt *BadDecl *BadExpr *BadStmt *BasicLit *BinaryExpr *BlockStmt *BranchStmt *CallExpr *CaseClause *ChanType *CommClause *Comment *CommentGroup *CompositeLit Decl (interface) *DeclStmt *DeferStmt *Ellipsis *EmptyStmt Expr (interface) *ExprStmt *Field *FieldList *File *ForStmt *FuncDecl *FuncLit *FuncType *GenDecl *GoStmt *Ident *IfStmt *ImportSpec *IncDecStmt *IndexExpr *InterfaceType *KeyValueExpr *LabeledStmt *MapType *Package *ParenExpr *RangeStmt *ReturnStmt *SelectorExpr *SelectStmt *SendStmt *SliceExpr Spec (interface) *StarExpr Stmt (interface) *StructType *SwitchStmt *TypeAssertExpr *TypeSpec *TypeSwitchStmt *UnaryExpr *ValueSpec *go/types.Scope func CommentMap.Update(old, new Node) Node func Inspect(node Node, f func(Node) bool) func NewCommentMap(fset *token.FileSet, node Node, comments []*CommentGroup) CommentMap func Walk(v Visitor, node Node) func CommentMap.Filter(node Node) CommentMap func CommentMap.Update(old, new Node) Node func Visitor.Visit(node Node) (w Visitor) func go/internal/typeparams.Get(Node) *FieldList func go/internal/typeparams.IsListExpr(n Node) bool func go/internal/typeparams.Set(node Node, params *FieldList)
An Object describes a named language entity such as a package, constant, type, variable, function (incl. methods), or label. The Data fields contains object-specific data: Kind Data type Data value Pkg *Scope package scope Con int iota for the respective declaration // object-specific data; or nil // corresponding Field, XxxSpec, FuncDecl, LabeledStmt, AssignStmt, Scope; or nil Kind ObjKind // declared name // placeholder for type information; may be nil Pos computes the source position of the declaration of an object name. The result may be an invalid position if it cannot be computed (obj.Decl may be nil or not correct). func NewObj(kind ObjKind, name string) *Object func (*Scope).Insert(obj *Object) (alt *Object) func (*Scope).Lookup(name string) *Object func (*Scope).Insert(obj *Object) (alt *Object)
ObjKind describes what an object represents. ( T) String() string T : expvar.Var T : fmt.Stringer func NewObj(kind ObjKind, name string) *Object const Bad const Con const Fun const Lbl const Pkg const Typ const Var
A Package node represents a set of source files collectively building a Go package. // Go source files by filename // map of package id -> package object // package name // package scope across all files (*T) End() token.Pos (*T) Pos() token.Pos *T : Node func NewPackage(fset *token.FileSet, files map[string]*File, importer Importer, universe *Scope) (*Package, error) func go/parser.ParseDir(fset *token.FileSet, path string, filter func(fs.FileInfo) bool, mode parser.Mode) (pkgs map[string]*Package, first error) func FilterPackage(pkg *Package, f Filter) bool func MergePackageFiles(pkg *Package, mode MergeMode) *File func PackageExports(pkg *Package) bool func go/doc.New(pkg *Package, importPath string, mode doc.Mode) *doc.Package
A ParenExpr node represents a parenthesized expression. // position of "(" // position of ")" // parenthesized expression (*T) End() token.Pos (*T) Pos() token.Pos *T : Expr *T : Node
A RangeStmt represents a for statement with a range clause. Body *BlockStmt // position of "for" keyword // Key, Value may be nil // ILLEGAL if Key == nil, ASSIGN, DEFINE // position of Tok; invalid if Key == nil // Key, Value may be nil // value to range over (*T) End() token.Pos (*T) Pos() token.Pos *T : Node *T : Stmt
A ReturnStmt node represents a return statement. // result expressions; or nil // position of "return" keyword (*T) End() token.Pos (*T) Pos() token.Pos *T : Node *T : Stmt
A Scope maintains the set of named language entities declared in the scope and a link to the immediately surrounding (outer) scope. Objects map[string]*Object Outer *Scope Insert attempts to insert a named object obj into the scope s. If the scope already contains an object alt with the same name, Insert leaves the scope unchanged and returns alt. Otherwise it inserts obj and returns nil. Lookup returns the object with the given name if it is found in scope s, otherwise it returns nil. Outer scopes are ignored. Debugging support *T : expvar.Var *T : fmt.Stringer func NewScope(outer *Scope) *Scope func NewPackage(fset *token.FileSet, files map[string]*File, importer Importer, universe *Scope) (*Package, error) func NewScope(outer *Scope) *Scope
A SelectorExpr node represents an expression followed by a selector. // field selector // expression (*T) End() token.Pos (*T) Pos() token.Pos *T : Expr *T : Node
A SelectStmt node represents a select statement. // CommClauses only // position of "select" keyword (*T) End() token.Pos (*T) Pos() token.Pos *T : Node *T : Stmt
A SendStmt node represents a send statement. // position of "<-" Chan Expr Value Expr (*T) End() token.Pos (*T) Pos() token.Pos *T : Node *T : Stmt
A SliceExpr node represents an expression followed by slice indices. // end of slice range; or nil // position of "[" // begin of slice range; or nil // maximum capacity of slice; or nil // position of "]" // true if 3-index slice (2 colons present) // expression (*T) End() token.Pos (*T) Pos() token.Pos *T : Expr *T : Node
The Spec type stands for any of *ImportSpec, *ValueSpec, and *TypeSpec. // position of first character immediately after the node // position of first character belonging to the node *ImportSpec *TypeSpec *ValueSpec T : Node
A StarExpr node represents an expression of the form "*" Expression. Semantically it could be a unary "*" expression, or a pointer type. // position of "*" // operand (*T) End() token.Pos (*T) Pos() token.Pos *T : Expr *T : Node
All statement nodes implement the Stmt interface. // position of first character immediately after the node // position of first character belonging to the node *AssignStmt *BadStmt *BlockStmt *BranchStmt *CaseClause *CommClause *DeclStmt *DeferStmt *EmptyStmt *ExprStmt *ForStmt *GoStmt *IfStmt *IncDecStmt *LabeledStmt *RangeStmt *ReturnStmt *SelectStmt *SendStmt *SwitchStmt *TypeSwitchStmt T : Node
A StructType node represents a struct type. // list of field declarations // true if (source) fields are missing in the Fields list // position of "struct" keyword (*T) End() token.Pos (*T) Pos() token.Pos *T : Expr *T : Node
A SwitchStmt node represents an expression switch statement. // CaseClauses only // initialization statement; or nil // position of "switch" keyword // tag expression; or nil (*T) End() token.Pos (*T) Pos() token.Pos *T : Node *T : Stmt
A TypeAssertExpr node represents an expression followed by a type assertion. // position of "(" // position of ")" // asserted type; nil means type switch X.(type) // expression (*T) End() token.Pos (*T) Pos() token.Pos *T : Expr *T : Node
A TypeSpec node represents a type declaration (TypeSpec production). // position of '=', if any // line comments; or nil // associated documentation; or nil // type name // *Ident, *ParenExpr, *SelectorExpr, *StarExpr, or any of the *XxxTypes (*T) End() token.Pos (*T) Pos() token.Pos *T : Node *T : Spec
A TypeSwitchStmt node represents a type switch statement. // x := y.(type) or y.(type) // CaseClauses only // initialization statement; or nil // position of "switch" keyword (*T) End() token.Pos (*T) Pos() token.Pos *T : Node *T : Stmt
A UnaryExpr node represents a unary expression. Unary "*" expressions are represented via StarExpr nodes. // operator // position of Op // operand (*T) End() token.Pos (*T) Pos() token.Pos *T : Expr *T : Node
A ValueSpec node represents a constant or variable declaration (ConstSpec or VarSpec production). // line comments; or nil // associated documentation; or nil // value names (len(Names) > 0) // value type; or nil // initial values; or nil (*T) End() token.Pos (*T) Pos() token.Pos *T : Node *T : Spec
A Visitor's Visit method is invoked for each node encountered by Walk. If the result visitor w is not nil, Walk visits each of the children of node with the visitor w, followed by a call of w.Visit(nil). ( T) Visit(node Node) (w Visitor) func Visitor.Visit(node Node) (w Visitor) func Walk(v Visitor, node Node)
Package-Level Functions (total 18)
FileExports trims the AST for a Go source file in place such that only exported nodes remain: all top-level identifiers which are not exported and their associated information (such as type, initial value, or function body) are removed. Non-exported fields and methods of exported types are stripped. The File.Comments list is not changed. FileExports reports whether there are exported declarations.
FilterDecl trims the AST for a Go declaration in place by removing all names (including struct field and interface method names, but not from parameter lists) that don't pass through the filter f. FilterDecl reports whether there are any declared names left after filtering.
FilterFile trims the AST for a Go file in place by removing all names from top-level declarations (including struct field and interface method names, but not from parameter lists) that don't pass through the filter f. If the declaration is empty afterwards, the declaration is removed from the AST. Import declarations are always removed. The File.Comments list is not changed. FilterFile reports whether there are any top-level declarations left after filtering.
FilterPackage trims the AST for a Go package in place by removing all names from top-level declarations (including struct field and interface method names, but not from parameter lists) that don't pass through the filter f. If the declaration is empty afterwards, the declaration is removed from the AST. The pkg.Files list is not changed, so that file names and top-level package comments don't get lost. FilterPackage reports whether there are any top-level declarations left after filtering.
Fprint prints the (sub-)tree starting at AST node x to w. If fset != nil, position information is interpreted relative to that file set. Otherwise positions are printed as integer values (file set specific offsets). A non-nil FieldFilter f may be provided to control the output: struct fields for which f(fieldname, fieldvalue) is true are printed; all others are filtered from the output. Unexported struct fields are never printed.
Inspect traverses an AST in depth-first order: It starts by calling f(node); node must not be nil. If f returns true, Inspect invokes f recursively for each of the non-nil children of node, followed by a call of f(nil).
IsExported reports whether name starts with an upper-case letter.
MergePackageFiles creates a file AST by merging the ASTs of the files belonging to a package. The mode flags control merging behavior.
NewCommentMap creates a new comment map by associating comment groups of the comments list with the nodes of the AST specified by node. A comment group g is associated with a node n if: - g starts on the same line as n ends - g starts on the line immediately following n, and there is at least one empty line after g and before the next node - g starts before n and is not associated to the node before n via the previous rules NewCommentMap tries to associate a comment group to the "largest" node possible: For instance, if the comment is a line comment trailing an assignment, the comment is associated with the entire assignment rather than just the last operand in the assignment.
NewIdent creates a new Ident without position. Useful for ASTs generated by code other than the Go parser.
NewObj creates a new object of a given kind and name.
NewPackage creates a new Package node from a set of File nodes. It resolves unresolved identifiers across files and updates each file's Unresolved list accordingly. If a non-nil importer and universe scope are provided, they are used to resolve identifiers not declared in any of the package files. Any remaining unresolved identifiers are reported as undeclared. If the files belong to different packages, one package name is selected and files with different package names are reported and then ignored. The result is a package node and a scanner.ErrorList if there were errors.
NewScope creates a new scope nested in the outer scope.
NotNilFilter returns true for field values that are not nil; it returns false otherwise.
PackageExports trims the AST for a Go package in place such that only exported nodes remain. The pkg.Files list is not changed, so that file names and top-level package comments don't get lost. PackageExports reports whether there are exported declarations; it returns false otherwise.
Print prints x to standard output, skipping nil fields. Print(fset, x) is the same as Fprint(os.Stdout, fset, x, NotNilFilter).
SortImports sorts runs of consecutive import lines in import blocks in f. It also removes duplicate imports when it is possible to do so without data loss.
Walk traverses an AST in depth-first order: It starts by calling v.Visit(node); node must not be nil. If the visitor w returned by v.Visit(node) is not nil, Walk is invoked recursively with visitor w for each of the non-nil children of node, followed by a call of w.Visit(nil).
Package-Level Constants (total 12)
The list of possible Object kinds.
The list of possible Object kinds.
If set, duplicate function declarations are excluded.
If set, duplicate import declarations are excluded.
If set, comments that are not associated with a specific AST node (as Doc or Comment) are excluded.
The list of possible Object kinds.
The list of possible Object kinds.
The list of possible Object kinds.
const RECV ChanDir = 2
const SEND ChanDir = 1
The list of possible Object kinds.
The list of possible Object kinds.