Documentation
¶
Overview ¶
Package ast declares the types used to represent syntax trees for Go packages.
Index ¶
- Constants
- 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 any, f FieldFilter) error
- func Inspect(node Node, f func(Node) bool)
- func IsExported(name string) bool
- func IsGenerated(file *File) bool
- func NotNilFilter(_ string, v reflect.Value) bool
- func PackageExports(pkg *Package) bool
- func Preorder(root Node) iter.Seq[Node]
- func Print(fset *token.FileSet, x any) error
- func SortImports(fset *token.FileSet, f *File)
- func Walk(v Visitor, node Node)
- type ArrayType
- type AssignStmt
- type BadDecl
- type BadExpr
- type BadStmt
- type BasicLit
- type BinaryExpr
- type BlockStmt
- type BranchStmt
- type CallExpr
- type CaseClause
- type ChanDir
- type ChanType
- type CommClause
- type Comment
- type CommentGroup
- type CommentMap
- type CompositeLit
- type Decl
- type DeclStmt
- type DeferStmt
- type Ellipsis
- type EmptyStmt
- type Expr
- type ExprStmt
- type Field
- type FieldFilter
- type FieldList
- type File
- type Filter
- type ForStmt
- type FuncDecl
- type FuncLit
- type FuncType
- type GenDecl
- type GoStmt
- type Ident
- type IfStmt
- type ImportSpec
- type Importerdeprecated
- type IncDecStmt
- type IndexExpr
- type IndexListExpr
- type InterfaceType
- type KeyValueExpr
- type LabeledStmt
- type MapType
- type MergeMode
- type Node
- type ObjKind
- type Objectdeprecated
- type Packagedeprecated
- type ParenExpr
- type Printer
- type RangeStmt
- type ReturnStmt
- type Scopedeprecated
- type SelectStmt
- type SelectorExpr
- type SendStmt
- type SliceExpr
- type Spec
- type StarExpr
- type Stmt
- type StructType
- type SwitchStmt
- type TypeAssertExpr
- type TypeSpec
- type TypeSwitchStmt
- type UnaryExpr
- type ValueSpec
- type Visitor
Constants ¶
const Bad = ast.Bad
const Con = ast.Con
const FilterFuncDuplicates = ast.FilterFuncDuplicates
If set, duplicate function declarations are excluded.
const FilterImportDuplicates = ast.FilterImportDuplicates
If set, duplicate import declarations are excluded.
const FilterUnassociatedComments = ast.FilterUnassociatedComments
If set, comments that are not associated with a specific AST node (as Doc or Comment) are excluded.
const Fun = ast.Fun
const Lbl = ast.Lbl
const Pkg = ast.Pkg
const RECV = ast.RECV
const SEND = ast.SEND
const Typ = ast.Typ
const Var = ast.Var
Variables ¶
This section is empty.
Functions ¶
func FileExports ¶
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. This is an alias of https://pkg.go.dev/go/ast#FileExports.
func FilterDecl ¶
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. This is an alias of https://pkg.go.dev/go/ast#FilterDecl.
func FilterFile ¶
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. This is an alias of https://pkg.go.dev/go/ast#FilterFile.
func FilterPackage ¶
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. This is an alias of https://pkg.go.dev/go/ast#FilterPackage.
func Fprint ¶
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. This is an alias of https://pkg.go.dev/go/ast#Fprint.
func Inspect ¶
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). This is an alias of https://pkg.go.dev/go/ast#Inspect.
func IsExported ¶
IsExported reports whether name starts with an upper-case letter. This is an alias of https://pkg.go.dev/go/ast#IsExported.
func IsGenerated ¶
IsGenerated reports whether the file was generated by a program, not handwritten, by detecting the special comment described at https://go.dev/s/generatedcode.
The syntax tree must have been parsed with the [parser.ParseComments] flag. Example:
f, err := parser.ParseFile(fset, filename, src, parser.ParseComments|parser.PackageClauseOnly)
if err != nil { ... }
gen := ast.IsGenerated(f)
This is an alias of https://pkg.go.dev/go/ast#IsGenerated.
func NotNilFilter ¶
NotNilFilter is a FieldFilter that returns true for field values that are not nil; it returns false otherwise. This is an alias of https://pkg.go.dev/go/ast#NotNilFilter.
func PackageExports ¶
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. This is an alias of https://pkg.go.dev/go/ast#PackageExports.
func Preorder ¶
Preorder returns an iterator over all the nodes of the syntax tree beneath (and including) the specified root, in depth-first preorder.
For greater control over the traversal of each subtree, use Inspect. This is an alias of https://pkg.go.dev/go/ast#Preorder.
func Print ¶
Print prints x to standard output, skipping nil fields. Print(fset, x) is the same as Fprint(os.Stdout, fset, x, NotNilFilter). This is an alias of https://pkg.go.dev/go/ast#Print.
func SortImports ¶
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. This is an alias of https://pkg.go.dev/go/ast#SortImports.
func Walk ¶
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). This is an alias of https://pkg.go.dev/go/ast#Walk.
Types ¶
type ArrayType ¶
A type is represented by a tree consisting of one or more of the following type-specific expression nodes.
This is an alias of https://pkg.go.dev/go/ast#ArrayType.
type AssignStmt ¶
type AssignStmt = ast.AssignStmt
A statement is represented by a tree consisting of one or more of the following concrete statement nodes.
This is an alias of https://pkg.go.dev/go/ast#AssignStmt.
type BadDecl ¶
A declaration is represented by one of the following declaration nodes.
This is an alias of https://pkg.go.dev/go/ast#BadDecl.
type BadExpr ¶
An expression is represented by a tree consisting of one or more of the following concrete expression nodes.
This is an alias of https://pkg.go.dev/go/ast#BadExpr.
type BadStmt ¶
A statement is represented by a tree consisting of one or more of the following concrete statement nodes.
This is an alias of https://pkg.go.dev/go/ast#BadStmt.
type BasicLit ¶
An expression is represented by a tree consisting of one or more of the following concrete expression nodes.
This is an alias of https://pkg.go.dev/go/ast#BasicLit.
type BinaryExpr ¶
type BinaryExpr = ast.BinaryExpr
An expression is represented by a tree consisting of one or more of the following concrete expression nodes.
This is an alias of https://pkg.go.dev/go/ast#BinaryExpr.
type BlockStmt ¶
A statement is represented by a tree consisting of one or more of the following concrete statement nodes.
This is an alias of https://pkg.go.dev/go/ast#BlockStmt.
type BranchStmt ¶
type BranchStmt = ast.BranchStmt
A statement is represented by a tree consisting of one or more of the following concrete statement nodes.
This is an alias of https://pkg.go.dev/go/ast#BranchStmt.
type CallExpr ¶
An expression is represented by a tree consisting of one or more of the following concrete expression nodes.
This is an alias of https://pkg.go.dev/go/ast#CallExpr.
type CaseClause ¶
type CaseClause = ast.CaseClause
A statement is represented by a tree consisting of one or more of the following concrete statement nodes.
This is an alias of https://pkg.go.dev/go/ast#CaseClause.
type ChanDir ¶
The direction of a channel type is indicated by a bit mask including one or both of the following constants.
This is an alias of https://pkg.go.dev/go/ast#ChanDir.
type ChanType ¶
A type is represented by a tree consisting of one or more of the following type-specific expression nodes.
This is an alias of https://pkg.go.dev/go/ast#ChanType.
type CommClause ¶
type CommClause = ast.CommClause
A statement is represented by a tree consisting of one or more of the following concrete statement nodes.
This is an alias of https://pkg.go.dev/go/ast#CommClause.
type Comment ¶
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 [Comment.End] does not match the true source end position for comments containing carriage returns.
This is an alias of https://pkg.go.dev/go/ast#Comment.
type CommentGroup ¶
type CommentGroup = ast.CommentGroup
A CommentGroup represents a sequence of comments with no other tokens and no empty lines between.
This is an alias of https://pkg.go.dev/go/ast#CommentGroup.
type CommentMap ¶
type CommentMap = ast.CommentMap
A CommentMap maps an AST node to a list of comment groups associated with it. See NewCommentMap for a description of the association.
This is an alias of https://pkg.go.dev/go/ast#CommentMap.
func NewCommentMap ¶
func NewCommentMap(fset *token.FileSet, node Node, comments []*CommentGroup) CommentMap
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. This is an alias of https://pkg.go.dev/go/ast#NewCommentMap.
type CompositeLit ¶
type CompositeLit = ast.CompositeLit
An expression is represented by a tree consisting of one or more of the following concrete expression nodes.
This is an alias of https://pkg.go.dev/go/ast#CompositeLit.
type Decl ¶
All declaration nodes implement the Decl interface.
This is an alias of https://pkg.go.dev/go/ast#Decl.
type DeclStmt ¶
A statement is represented by a tree consisting of one or more of the following concrete statement nodes.
This is an alias of https://pkg.go.dev/go/ast#DeclStmt.
type DeferStmt ¶
A statement is represented by a tree consisting of one or more of the following concrete statement nodes.
This is an alias of https://pkg.go.dev/go/ast#DeferStmt.
type Ellipsis ¶
An expression is represented by a tree consisting of one or more of the following concrete expression nodes.
This is an alias of https://pkg.go.dev/go/ast#Ellipsis.
type EmptyStmt ¶
A statement is represented by a tree consisting of one or more of the following concrete statement nodes.
This is an alias of https://pkg.go.dev/go/ast#EmptyStmt.
type Expr ¶
All expression nodes implement the Expr interface.
This is an alias of https://pkg.go.dev/go/ast#Expr.
func Unparen ¶
Unparen returns the expression with any enclosing parentheses removed. This is an alias of https://pkg.go.dev/go/ast#Unparen.
type ExprStmt ¶
A statement is represented by a tree consisting of one or more of the following concrete statement nodes.
This is an alias of https://pkg.go.dev/go/ast#ExprStmt.
type Field ¶
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.
This is an alias of https://pkg.go.dev/go/ast#Field.
type FieldFilter ¶
type FieldFilter = ast.FieldFilter
A FieldFilter may be provided to Fprint to control the output.
This is an alias of https://pkg.go.dev/go/ast#FieldFilter.
type FieldList ¶
A FieldList represents a list of Fields, enclosed by parentheses, curly braces, or square brackets.
This is an alias of https://pkg.go.dev/go/ast#FieldList.
type File ¶
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).
This is an alias of https://pkg.go.dev/go/ast#File.
func MergePackageFiles ¶
MergePackageFiles creates a file AST by merging the ASTs of the files belonging to a package. The mode flags control merging behavior. This is an alias of https://pkg.go.dev/go/ast#MergePackageFiles.
type ForStmt ¶
A statement is represented by a tree consisting of one or more of the following concrete statement nodes.
This is an alias of https://pkg.go.dev/go/ast#ForStmt.
type FuncDecl ¶
A declaration is represented by one of the following declaration nodes.
This is an alias of https://pkg.go.dev/go/ast#FuncDecl.
type FuncLit ¶
An expression is represented by a tree consisting of one or more of the following concrete expression nodes.
This is an alias of https://pkg.go.dev/go/ast#FuncLit.
type FuncType ¶
A type is represented by a tree consisting of one or more of the following type-specific expression nodes.
This is an alias of https://pkg.go.dev/go/ast#FuncType.
type GenDecl ¶
A declaration is represented by one of the following declaration nodes.
This is an alias of https://pkg.go.dev/go/ast#GenDecl.
type GoStmt ¶
A statement is represented by a tree consisting of one or more of the following concrete statement nodes.
This is an alias of https://pkg.go.dev/go/ast#GoStmt.
type Ident ¶
An expression is represented by a tree consisting of one or more of the following concrete expression nodes.
This is an alias of https://pkg.go.dev/go/ast#Ident.
type IfStmt ¶
A statement is represented by a tree consisting of one or more of the following concrete statement nodes.
This is an alias of https://pkg.go.dev/go/ast#IfStmt.
type ImportSpec ¶
type ImportSpec = ast.ImportSpec
A Spec node represents a single (non-parenthesized) import, constant, type, or variable declaration.
This is an alias of https://pkg.go.dev/go/ast#ImportSpec.
type Importer
deprecated
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.
Deprecated: use the type checker go/types instead; see Object.
This is an alias of https://pkg.go.dev/go/ast#Importer.
type IncDecStmt ¶
type IncDecStmt = ast.IncDecStmt
A statement is represented by a tree consisting of one or more of the following concrete statement nodes.
This is an alias of https://pkg.go.dev/go/ast#IncDecStmt.
type IndexExpr ¶
An expression is represented by a tree consisting of one or more of the following concrete expression nodes.
This is an alias of https://pkg.go.dev/go/ast#IndexExpr.
type IndexListExpr ¶
type IndexListExpr = ast.IndexListExpr
An expression is represented by a tree consisting of one or more of the following concrete expression nodes.
This is an alias of https://pkg.go.dev/go/ast#IndexListExpr.
type InterfaceType ¶
type InterfaceType = ast.InterfaceType
A type is represented by a tree consisting of one or more of the following type-specific expression nodes.
This is an alias of https://pkg.go.dev/go/ast#InterfaceType.
type KeyValueExpr ¶
type KeyValueExpr = ast.KeyValueExpr
An expression is represented by a tree consisting of one or more of the following concrete expression nodes.
This is an alias of https://pkg.go.dev/go/ast#KeyValueExpr.
type LabeledStmt ¶
type LabeledStmt = ast.LabeledStmt
A statement is represented by a tree consisting of one or more of the following concrete statement nodes.
This is an alias of https://pkg.go.dev/go/ast#LabeledStmt.
type MapType ¶
A type is represented by a tree consisting of one or more of the following type-specific expression nodes.
This is an alias of https://pkg.go.dev/go/ast#MapType.
type MergeMode ¶
The MergeMode flags control the behavior of MergePackageFiles.
This is an alias of https://pkg.go.dev/go/ast#MergeMode.
type Node ¶
All node types implement the Node interface.
This is an alias of https://pkg.go.dev/go/ast#Node.
type ObjKind ¶
ObjKind describes what an Object represents.
This is an alias of https://pkg.go.dev/go/ast#ObjKind.
type Object
deprecated
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
Deprecated: The relationship between Idents and Objects cannot be correctly computed without type information. For example, the expression T{K: 0} may denote a struct, map, slice, or array literal, depending on the type of T. If T is a struct, then K refers to a field of T, whereas for the other types it refers to a value in the environment.
New programs should set the [parser.SkipObjectResolution] parser flag to disable syntactic object resolution (which also saves CPU and memory), and instead use the type checker go/types if object resolution is desired. See the Defs, Uses, and Implicits fields of the [types.Info] struct for details.
This is an alias of https://pkg.go.dev/go/ast#Object.
type Package
deprecated
A Package node represents a set of source files collectively building a Go package.
Deprecated: use the type checker go/types instead; see Object.
This is an alias of https://pkg.go.dev/go/ast#Package.
func NewPackage
deprecated
func NewPackage(fset *token.FileSet, files map[string]*File, importer Importer, universe *Scope) (*Package, error)
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.
Deprecated: use the type checker go/types instead; see Object. This is an alias of https://pkg.go.dev/go/ast#NewPackage.
type ParenExpr ¶
An expression is represented by a tree consisting of one or more of the following concrete expression nodes.
This is an alias of https://pkg.go.dev/go/ast#ParenExpr.
type RangeStmt ¶
A statement is represented by a tree consisting of one or more of the following concrete statement nodes.
This is an alias of https://pkg.go.dev/go/ast#RangeStmt.
type ReturnStmt ¶
type ReturnStmt = ast.ReturnStmt
A statement is represented by a tree consisting of one or more of the following concrete statement nodes.
This is an alias of https://pkg.go.dev/go/ast#ReturnStmt.
type Scope
deprecated
A Scope maintains the set of named language entities declared in the scope and a link to the immediately surrounding (outer) scope.
Deprecated: use the type checker go/types instead; see Object.
This is an alias of https://pkg.go.dev/go/ast#Scope.
func NewScope ¶
NewScope creates a new scope nested in the outer scope. This is an alias of https://pkg.go.dev/go/ast#NewScope.
type SelectStmt ¶
type SelectStmt = ast.SelectStmt
A statement is represented by a tree consisting of one or more of the following concrete statement nodes.
This is an alias of https://pkg.go.dev/go/ast#SelectStmt.
type SelectorExpr ¶
type SelectorExpr = ast.SelectorExpr
An expression is represented by a tree consisting of one or more of the following concrete expression nodes.
This is an alias of https://pkg.go.dev/go/ast#SelectorExpr.
type SendStmt ¶
A statement is represented by a tree consisting of one or more of the following concrete statement nodes.
This is an alias of https://pkg.go.dev/go/ast#SendStmt.
type SliceExpr ¶
An expression is represented by a tree consisting of one or more of the following concrete expression nodes.
This is an alias of https://pkg.go.dev/go/ast#SliceExpr.
type Spec ¶
A Spec node represents a single (non-parenthesized) import, constant, type, or variable declaration.
This is an alias of https://pkg.go.dev/go/ast#Spec.
type StarExpr ¶
An expression is represented by a tree consisting of one or more of the following concrete expression nodes.
This is an alias of https://pkg.go.dev/go/ast#StarExpr.
type Stmt ¶
All statement nodes implement the Stmt interface.
This is an alias of https://pkg.go.dev/go/ast#Stmt.
type StructType ¶
type StructType = ast.StructType
A type is represented by a tree consisting of one or more of the following type-specific expression nodes.
This is an alias of https://pkg.go.dev/go/ast#StructType.
type SwitchStmt ¶
type SwitchStmt = ast.SwitchStmt
A statement is represented by a tree consisting of one or more of the following concrete statement nodes.
This is an alias of https://pkg.go.dev/go/ast#SwitchStmt.
type TypeAssertExpr ¶
type TypeAssertExpr = ast.TypeAssertExpr
An expression is represented by a tree consisting of one or more of the following concrete expression nodes.
This is an alias of https://pkg.go.dev/go/ast#TypeAssertExpr.
type TypeSpec ¶
A Spec node represents a single (non-parenthesized) import, constant, type, or variable declaration.
This is an alias of https://pkg.go.dev/go/ast#TypeSpec.
type TypeSwitchStmt ¶
type TypeSwitchStmt = ast.TypeSwitchStmt
A statement is represented by a tree consisting of one or more of the following concrete statement nodes.
This is an alias of https://pkg.go.dev/go/ast#TypeSwitchStmt.
type UnaryExpr ¶
An expression is represented by a tree consisting of one or more of the following concrete expression nodes.
This is an alias of https://pkg.go.dev/go/ast#UnaryExpr.
type ValueSpec ¶
A Spec node represents a single (non-parenthesized) import, constant, type, or variable declaration.
This is an alias of https://pkg.go.dev/go/ast#ValueSpec.
type Visitor ¶
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).
This is an alias of https://pkg.go.dev/go/ast#Visitor.