Overview

Package ast declares the types used to represent syntax trees for Go packages.

Index

Package files

ast.go filter.go print.go scope.go

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.

func FilterDecl

  1. func FilterDecl(decl Decl, f ) bool

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.

func

  1. func FilterFile(src *, f Filter)

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.

func FilterPackage

  1. func FilterPackage(pkg *Package, f ) bool

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.

func

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

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.

func

  1. func Inspect(node , f func(Node) )

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).


Example:

  1. // src is the input for which we want to inspect the AST.
  2. src := `
  3. package p
  4. const c = 1.0
  5. var X = f(3.14)*2 + c
  6. `
  7. // Create the AST by parsing src.
  8. fset := token.NewFileSet() // positions are relative to fset
  9. f, err := parser.ParseFile(fset, "src.go", src, 0)
  10. if err != nil {
  11. panic(err)
  12. }
  13. // Inspect the AST and print all identifiers and literals.
  14. ast.Inspect(f, func(n ast.Node) bool {
  15. var s string
  16. switch x := n.(type) {
  17. case *ast.BasicLit:
  18. s = x.Value
  19. case *ast.Ident:
  20. s = x.Name
  21. }
  22. if s != "" {
  23. fmt.Printf("%s:\t%s\n", fset.Position(n.Pos()), s)
  24. }
  25. return true
  26. })
  27. // Output:
  28. // src.go:2:9: p
  29. // src.go:3:7: c
  30. // src.go:3:11: 1.0
  31. // src.go:4:5: X
  32. // src.go:4:9: f
  33. // src.go:4:11: 3.14
  34. // src.go:4:17: 2
  35. // src.go:4:21: c

func

  1. func IsExported(name ) bool

IsExported reports whether name is an exported Go symbol (that is, whether it
begins with an upper-case letter).

func

  1. func NotNilFilter(_ , v reflect.) bool

NotNilFilter returns true for field values that are not nil; it returns false
otherwise.

func

  1. func PackageExports(pkg *) bool

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.

func

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

Print prints x to standard output, skipping nil fields. Print(fset, x) is the
same as Fprint(os.Stdout, fset, x, NotNilFilter).


Example:

  1. // src is the input for which we want to print the AST.
  2. src := `
  3. package main
  4. func main() {
  5. println("Hello, World!")
  6. }
  7. `
  8. // Create the AST by parsing src.
  9. fset := token.NewFileSet() // positions are relative to fset
  10. f, err := parser.ParseFile(fset, "", src, 0)
  11. if err != nil {
  12. panic(err)
  13. }
  14. // Print the AST.
  15. ast.Print(fset, f)
  16. // Output:
  17. // 0 *ast.File {
  18. // 1 . Package: 2:1
  19. // 2 . Name: *ast.Ident {
  20. // 3 . . NamePos: 2:9
  21. // 4 . . Name: "main"
  22. // 6 . Decls: []ast.Decl (len = 1) {
  23. // 7 . . 0: *ast.FuncDecl {
  24. // 8 . . . Name: *ast.Ident {
  25. // 9 . . . . NamePos: 3:6
  26. // 10 . . . . Name: "main"
  27. // 11 . . . . Obj: *ast.Object {
  28. // 12 . . . . . Kind: func
  29. // 13 . . . . . Name: "main"
  30. // 15 . . . . }
  31. // 16 . . . }
  32. // 17 . . . Type: *ast.FuncType {
  33. // 18 . . . . Func: 3:1
  34. // 19 . . . . Params: *ast.FieldList {
  35. // 20 . . . . . Opening: 3:10
  36. // 21 . . . . . Closing: 3:11
  37. // 22 . . . . }
  38. // 23 . . . }
  39. // 24 . . . Body: *ast.BlockStmt {
  40. // 25 . . . . Lbrace: 3:13
  41. // 26 . . . . List: []ast.Stmt (len = 1) {
  42. // 27 . . . . . 0: *ast.ExprStmt {
  43. // 28 . . . . . . X: *ast.CallExpr {
  44. // 29 . . . . . . . Fun: *ast.Ident {
  45. // 30 . . . . . . . . NamePos: 4:2
  46. // 31 . . . . . . . . Name: "println"
  47. // 32 . . . . . . . }
  48. // 33 . . . . . . . Lparen: 4:9
  49. // 34 . . . . . . . Args: []ast.Expr (len = 1) {
  50. // 35 . . . . . . . . 0: *ast.BasicLit {
  51. // 36 . . . . . . . . . ValuePos: 4:10
  52. // 37 . . . . . . . . . Kind: STRING
  53. // 38 . . . . . . . . . Value: "\"Hello, World!\""
  54. // 39 . . . . . . . . }
  55. // 40 . . . . . . . }
  56. // 41 . . . . . . . Ellipsis: -
  57. // 42 . . . . . . . Rparen: 4:25
  58. // 43 . . . . . . }
  59. // 44 . . . . . }
  60. // 45 . . . . }
  61. // 46 . . . . Rbrace: 5:1
  62. // 47 . . . }
  63. // 48 . . }
  64. // 49 . }
  65. // 50 . Scope: *ast.Scope {
  66. // 51 . . Objects: map[string]*ast.Object (len = 1) {
  67. // 52 . . . "main": *(obj @ 11)
  68. // 53 . . }
  69. // 54 . }
  70. // 55 . Unresolved: []*ast.Ident (len = 1) {
  71. // 56 . . 0: *(obj @ 29)
  72. // 57 . }
  73. // 58 }

func

  1. func SortImports(fset *.FileSet, f *)

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.

func Walk

  1. func Walk(v Visitor, node )

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).

type ArrayType

  1. type ArrayType struct {
  2. Lbrack token. // position of "["
  3. Len Expr // Ellipsis node for [...]T array types, nil for slice types
  4. Elt // element type
  5. }

An ArrayType node represents an array or slice type.

func (*ArrayType) End

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

func (*ArrayType)

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

type AssignStmt

  1. type AssignStmt struct {
  2. Lhs []Expr
  3. TokPos .Pos // position of Tok
  4. Tok .Token // assignment token, DEFINE
  5. Rhs []
  6. }

An AssignStmt node represents an assignment or a short variable declaration.

func (*AssignStmt) End

  1. func (s *AssignStmt) End() .Pos

func (*AssignStmt)

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

type BadDecl

  1. type BadDecl struct {
  2. From, To token. // position range of bad declaration
  3. }

A BadDecl node is a placeholder for declarations containing syntax errors for
which no correct declaration nodes can be created.

func (*BadDecl) End

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

func (*BadDecl)

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

type BadExpr

  1. type BadExpr struct {
  2. From, To token. // position range of bad expression
  3. }

A BadExpr node is a placeholder for expressions containing syntax errors for
which no correct expression nodes can be created.

func (*BadExpr) End

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

func (*BadExpr)

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

type BadStmt

  1. type BadStmt struct {
  2. From, To token. // position range of bad statement
  3. }

A BadStmt node is a placeholder for statements containing syntax errors for
which no correct statement nodes can be created.

func (*BadStmt) End

  1. func (s *BadStmt) End() .Pos

func (*BadStmt)

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

type BasicLit

  1. type BasicLit struct {
  2. ValuePos token. // literal position
  3. Kind token. // token.INT, token.FLOAT, token.IMAG, token.CHAR, or token.STRING
  4. Value string // literal string; e.g. 42, 0x7f, 3.14, 1e-9, 2.4i, 'a', '\x7f', "foo" or `\m\n\o`
  5. }

A BasicLit node represents a literal of basic type.

func (*BasicLit)

  1. func (x *) End() token.

func (*BasicLit) Pos

  1. func (x *BasicLit) Pos() .Pos

type

  1. type BinaryExpr struct {
  2. X // left operand
  3. OpPos token. // position of Op
  4. Op token. // operator
  5. Y Expr // right operand
  6. }

A BinaryExpr node represents a binary expression.

func (*BinaryExpr)

  1. func (x *) End() token.

func (*BinaryExpr) Pos

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

type

  1. type BlockStmt struct {
  2. Lbrace .Pos // position of "{"
  3. List []
  4. Rbrace token. // position of "}"
  5. }

A BlockStmt node represents a braced statement list.

func (*BlockStmt) End

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

func (*BlockStmt)

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

type BranchStmt

  1. type BranchStmt struct {
  2. TokPos token. // position of Tok
  3. Tok token. // keyword token (BREAK, CONTINUE, GOTO, FALLTHROUGH)
  4. Label *Ident // label name; or nil
  5. }

A BranchStmt node represents a break, continue, goto, or fallthrough statement.

func (*BranchStmt)

  1. func (s *) End() token.

func (*BranchStmt) Pos

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

type

  1. type CallExpr struct {
  2. Fun // function expression
  3. Lparen token. // position of "("
  4. Args []Expr // function arguments; or nil
  5. Ellipsis .Pos // position of "..." (token.NoPos if there is no "...")
  6. Rparen .Pos // position of ")"
  7. }

A CallExpr node represents an expression followed by an argument list.

func (*CallExpr)

  1. func (x *) End() token.

func (*CallExpr) Pos

  1. func (x *CallExpr) Pos() .Pos

type

  1. type CaseClause struct {
  2. Case .Pos // position of "case" or "default" keyword
  3. List [] // list of expressions or types; nil means default case
  4. Colon token. // position of ":"
  5. Body []Stmt // statement list; or nil
  6. }

A CaseClause represents a case of an expression or type switch statement.

func (*CaseClause)

  1. func (s *) End() token.

func (*CaseClause) Pos

  1. func (s *CaseClause) Pos() .Pos

type

  1. type ChanDir

The direction of a channel type is indicated by a bit mask including one or both
of the following constants.

  1. const (
  2. SEND ChanDir = 1 <<
  3. RECV
  4. )

type ChanType

  1. type ChanType struct {
  2. Begin token. // position of "chan" keyword or "<-" (whichever comes first)
  3. Arrow token. // position of "<-" (token.NoPos if there is no "<-")
  4. Dir ChanDir // channel direction
  5. Value // value type
  6. }

A ChanType node represents a channel type.

func (*ChanType) End

  1. func (x *ChanType) End() .Pos

func (*ChanType)

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

  1. type CommClause struct {
  2. Case token. // position of "case" or "default" keyword
  3. Comm Stmt // send or receive statement; nil means default case
  4. Colon .Pos // position of ":"
  5. Body [] // statement list; or nil
  6. }

A CommClause node represents a case of a select statement.

func (*CommClause) End

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

func (*CommClause)

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

type Comment

  1. type Comment struct {
  2. Slash token. // position of "/" starting the comment
  3. Text string // comment text (excluding '\n' for //-style comments)
  4. }

A Comment node represents a single //-style or /*-style comment.

func (*Comment)

  1. func (c *) End() token.

func (*Comment) Pos

  1. func (c *Comment) Pos() .Pos

type

  1. type CommentGroup struct {
  2. List []* // len(List) > 0
  3. }

A CommentGroup represents a sequence of comments with no other tokens and no
empty lines between.

func (*CommentGroup) End

  1. func (g *CommentGroup) End() .Pos

func (*CommentGroup)

  1. func (g *) Pos() token.

func (*CommentGroup) Text

  1. func (g *CommentGroup) Text()

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.
Multiple empty lines are reduced to one, and trailing space on lines is trimmed.
Unless the result is empty, it is newline-terminated.

type CommentMap

  1. type CommentMap map[Node][]*

A CommentMap maps an AST node to a list of comment groups associated with it.
See NewCommentMap for a description of the association.


Example:

  1. // src is the input for which we create the AST that we
  2. // are going to manipulate.
  3. src := `
  4. // This is the package comment.
  5. package main
  6. // This comment is associated with the hello constant.
  7. const hello = "Hello, World!" // line comment 1
  8. var foo = hello // line comment 2
  9. // This comment is associated with the main function.
  10. func main() {
  11. fmt.Println(hello) // line comment 3
  12. }
  13. `
  14. // Create the AST by parsing src.
  15. fset := token.NewFileSet() // positions are relative to fset
  16. f, err := parser.ParseFile(fset, "src.go", src, parser.ParseComments)
  17. if err != nil {
  18. panic(err)
  19. }
  20. // Create an ast.CommentMap from the ast.File's comments.
  21. // This helps keeping the association between comments
  22. // and AST nodes.
  23. cmap := ast.NewCommentMap(fset, f, f.Comments)
  24. // Remove the first variable declaration from the list of declarations.
  25. for i, decl := range f.Decls {
  26. if gen, ok := decl.(*ast.GenDecl); ok && gen.Tok == token.VAR {
  27. copy(f.Decls[i:], f.Decls[i+1:])
  28. f.Decls = f.Decls[:len(f.Decls)-1]
  29. }
  30. }
  31. // Use the comment map to filter comments that don't belong anymore
  32. // (the comments associated with the variable declaration), and create
  33. // the new comments list.
  34. f.Comments = cmap.Filter(f).Comments()
  35. // Print the modified AST.
  36. var buf bytes.Buffer
  37. if err := format.Node(&buf, fset, f); err != nil {
  38. panic(err)
  39. }
  40. fmt.Printf("%s", buf.Bytes())
  41. // Output:
  42. // // This is the package comment.
  43. // package main
  44. //
  45. // // This comment is associated with the hello constant.
  46. // const hello = "Hello, World!" // line comment 1
  47. //
  48. // // This comment is associated with the main function.
  49. // func main() {
  50. // fmt.Println(hello) // line comment 3
  51. // }

func

  1. func NewCommentMap(fset *.FileSet, node , comments []*CommentGroup)

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:

  1. - g starts on the same line as n ends
  2. - g starts on the line immediately following n, and there is
  3. at least one empty line after g and before the next node
  4. - g starts before n and is not associated to the node before n
  5. 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.

func (CommentMap) Comments

  1. func (cmap CommentMap) Comments() []*

Comments returns the list of comment groups in the comment map. The result is
sorted in source order.

func (CommentMap) Filter

  1. func (cmap CommentMap) Filter(node ) CommentMap

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.

func (CommentMap)

  1. func (cmap ) String() string

func (CommentMap)

  1. func (cmap ) Update(old, new Node)

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.

type CompositeLit

  1. type CompositeLit struct {
  2. Type Expr // literal type; or nil
  3. Lbrace .Pos // position of "{"
  4. Elts [] // list of composite elements; or nil
  5. Rbrace token. // position of "}"
  6. }

A CompositeLit node represents a composite literal.

func (*CompositeLit) End

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

func (*CompositeLit)

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

type Decl

All declaration nodes implement the Decl interface.

type DeclStmt

  1. type DeclStmt struct {
  2. Decl Decl // *GenDecl with CONST, TYPE, or VAR token
  3. }

A DeclStmt node represents a declaration in a statement list.

func (*DeclStmt)

  1. func (s *) End() token.

func (*DeclStmt) Pos

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

type

  1. type DeferStmt struct {
  2. Defer .Pos // position of "defer" keyword
  3. Call *
  4. }

A DeferStmt node represents a defer statement.

func (*DeferStmt) End

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

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

type Ellipsis

  1. type Ellipsis struct {
  2. Ellipsis token. // position of "..."
  3. Elt Expr // ellipsis element type (parameter lists only); or nil
  4. }

An Ellipsis node stands for the “…” type in a parameter list or the “…”
length in an array type.

func (*Ellipsis)

  1. func (x *) End() token.

func (*Ellipsis) Pos

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

type

  1. type EmptyStmt struct {
  2. Semicolon .Pos // position of following ";"
  3. Implicit // if set, ";" was omitted in the source
  4. }

An EmptyStmt node represents an empty statement. The “position” of the empty
statement is the position of the immediately following (explicit or implicit)
semicolon.

func (*EmptyStmt) End

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

func (*EmptyStmt)

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

type Expr

  1. type Expr interface {
  2. Node
  3. // contains filtered or unexported methods
  4. }

All expression nodes implement the Expr interface.

type

  1. type ExprStmt struct {
  2. X // expression
  3. }

An ExprStmt node represents a (stand-alone) expression in a statement list.

func (*ExprStmt) End

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

func (*ExprStmt)

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

type Field

  1. type Field struct {
  2. Doc *CommentGroup // associated documentation; or nil
  3. Names []* // field/method/parameter names; or nil if anonymous field
  4. Type Expr // field/method/parameter type
  5. Tag * // field tag; or nil
  6. Comment *CommentGroup // line comments; or nil
  7. }

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.

func (*Field)

  1. func (f *) End() token.

func (*Field) Pos

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

type

  1. type FieldFilter func(name , value reflect.) bool

A FieldFilter may be provided to Fprint to control the output.

type

  1. type FieldList struct {
  2. Opening .Pos // position of opening parenthesis/brace, if any
  3. List []* // field list; or nil
  4. Closing token. // position of closing parenthesis/brace, if any
  5. }

A FieldList represents a list of Fields, enclosed by parentheses or braces.

func (*FieldList) End

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

func (*FieldList)

  1. func (f *) NumFields() int

NumFields returns the number of (named and anonymous fields) in a FieldList.

func (*FieldList)

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

type File

  1. type File struct {
  2. Doc *CommentGroup // associated documentation; or nil
  3. Package .Pos // position of "package" keyword
  4. Name * // package name
  5. Decls []Decl // top-level declarations; or nil
  6. Scope * // package scope (this file only)
  7. Imports []*ImportSpec // imports in this file
  8. Unresolved []* // unresolved identifiers in this file
  9. Comments []*CommentGroup // list of all comments in the source file
  10. }

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).

func

  1. func MergePackageFiles(pkg *, mode MergeMode) *

MergePackageFiles creates a file AST by merging the ASTs of the files belonging
to a package. The mode flags control merging behavior.

func (*File) End

  1. func (f *File) End() .Pos

func (*File)

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

type Filter

    type ForStmt

    1. type ForStmt struct {
    2. For token. // position of "for" keyword
    3. Init Stmt // initialization statement; or nil
    4. Cond // condition; or nil
    5. Post Stmt // post iteration statement; or nil
    6. Body *
    7. }

    A ForStmt represents a for statement.

    func (*ForStmt) End

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

    func (*ForStmt)

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

    type FuncDecl

    1. type FuncDecl struct {
    2. Doc *CommentGroup // associated documentation; or nil
    3. Recv * // receiver (methods); or nil (functions)
    4. Name *Ident // function/method name
    5. Type * // function signature: parameters, results, and position of "func" keyword
    6. Body *BlockStmt // function body; or nil for external (non-Go) function
    7. }

    A FuncDecl node represents a function declaration.

    func (*FuncDecl)

    1. func (d *) End() token.

    func (*FuncDecl) Pos

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

    type

    1. type FuncLit struct {
    2. Type * // function type
    3. Body *BlockStmt // function body
    4. }

    A FuncLit node represents a function literal.

    func (*FuncLit)

    1. func (x *) End() token.

    func (*FuncLit) Pos

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

    type

    1. type FuncType struct {
    2. Func .Pos // position of "func" keyword (token.NoPos if there is no "func")
    3. Params * // (incoming) parameters; non-nil
    4. Results *FieldList // (outgoing) results; or nil
    5. }

    A FuncType node represents a function type.

    func (*FuncType)

    1. func (x *) End() token.

    func (*FuncType) Pos

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

    type

    1. type GenDecl struct {
    2. Doc * // associated documentation; or nil
    3. TokPos token. // position of Tok
    4. Tok token. // IMPORT, CONST, TYPE, VAR
    5. Lparen token. // position of '(', if any
    6. Specs []Spec
    7. Rparen .Pos // position of ')', if any
    8. }

    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:

    1. token.IMPORT *ImportSpec
    2. token.CONST *ValueSpec
    3. token.TYPE *TypeSpec
    4. token.VAR *ValueSpec

    func (*GenDecl)

    1. func (d *) End() token.

    func (*GenDecl) Pos

    1. func (d *GenDecl) Pos() .Pos

    type

    1. type GoStmt struct {
    2. Go .Pos // position of "go" keyword
    3. Call *
    4. }

    A GoStmt node represents a go statement.

    func (*GoStmt) End

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

    func (*GoStmt)

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

    type Ident

    1. type Ident struct {
    2. NamePos token. // identifier position
    3. Name string // identifier name
    4. Obj * // denoted object; or nil
    5. }

    An Ident node represents an identifier.

    func NewIdent

    1. func NewIdent(name string) *

    NewIdent creates a new Ident without position. Useful for ASTs generated by code
    other than the Go parser.

    func (*Ident) End

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

    func (*Ident)

    1. func (id *) IsExported() bool

    IsExported reports whether id is an exported Go symbol (that is, whether it
    begins with an uppercase letter).

    func (*Ident)

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

    func (*Ident) String

    1. func (id *Ident) String()

    type IfStmt

    1. type IfStmt struct {
    2. If token. // position of "if" keyword
    3. Init Stmt // initialization statement; or nil
    4. Cond // condition
    5. Body *BlockStmt
    6. Else // else branch; or nil
    7. }

    An IfStmt node represents an if statement.

    func (*IfStmt) End

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

    func (*IfStmt)

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

    type ImportSpec

    1. type ImportSpec struct {
    2. Doc *CommentGroup // associated documentation; or nil
    3. Name * // local package name (including "."); or nil
    4. Path *BasicLit // import path
    5. Comment * // line comments; or nil
    6. EndPos token. // end of spec (overrides Path.Pos if nonzero)
    7. }

    An ImportSpec node represents a single package import.

    func (*ImportSpec) End

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

    func (*ImportSpec)

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

    type Importer

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

    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.

    type

    1. type IncDecStmt struct {
    2. X
    3. TokPos token. // position of Tok
    4. Tok token. // INC or DEC
    5. }

    An IncDecStmt node represents an increment or decrement statement.

    func (*IncDecStmt) End

    1. func (s *IncDecStmt) End() .Pos

    func (*IncDecStmt)

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

    1. type IndexExpr struct {
    2. X Expr // expression
    3. Lbrack .Pos // position of "["
    4. Index // index expression
    5. Rbrack token. // position of "]"
    6. }

    An IndexExpr node represents an expression followed by an index.

    func (*IndexExpr) End

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

    func (*IndexExpr)

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

    type InterfaceType

    1. type InterfaceType struct {
    2. Interface token. // position of "interface" keyword
    3. Methods *FieldList // list of methods
    4. Incomplete // true if (source) methods are missing in the Methods list
    5. }

    An InterfaceType node represents an interface type.

    func (*InterfaceType) End

    1. func (x *InterfaceType) End() .Pos

    func (*InterfaceType)

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

    type KeyValueExpr

    1. type KeyValueExpr struct {
    2. Key Expr
    3. Colon .Pos // position of ":"
    4. Value
    5. }

    func (*KeyValueExpr) End

    1. func (x *KeyValueExpr) End() .Pos

    type

    1. type LabeledStmt struct {
    2. Label *
    3. Colon token. // position of ":"
    4. Stmt Stmt
    5. }

    A LabeledStmt node represents a labeled statement.

    func (*LabeledStmt)

    1. func (s *) End() token.

    func (*LabeledStmt) Pos

    1. func (s *LabeledStmt) Pos() .Pos

    type

    1. type MapType struct {
    2. Map .Pos // position of "map" keyword
    3. Key
    4. Value Expr
    5. }

    A MapType node represents a map type.

    func (*MapType)

    1. func (x *) End() token.

    func (*MapType) Pos

    1. func (x *MapType) Pos() .Pos

    type

    1. type MergeMode

    The MergeMode flags control the behavior of MergePackageFiles.

    1. const (
    2. // If set, duplicate function declarations are excluded.
    3. FilterFuncDuplicates MergeMode = 1 <<
    4. // If set, comments that are not associated with a specific
    5. // AST node (as Doc or Comment) are excluded.
    6. FilterUnassociatedComments
    7. // If set, duplicate import declarations are excluded.
    8. FilterImportDuplicates
    9. )

    type Node

    1. type Node interface {
    2. Pos() token. // position of first character belonging to the node
    3. End() token. // position of first character immediately after the node
    4. }

    All node types implement the Node interface.

    type ObjKind

    1. type ObjKind int

    ObjKind describes what an object represents.

    1. const (
    2. Bad = iota // for error handling
    3. Pkg // package
    4. Con // constant
    5. Typ // type
    6. Var // variable
    7. Fun // function or method
    8. Lbl // label
    9. )

    The list of possible Object kinds.

    func (ObjKind)

    1. func (kind ) String() string

    type

    1. type Object struct {
    2. Kind
    3. Name string // declared name
    4. Decl interface{} // corresponding Field, XxxSpec, FuncDecl, LabeledStmt, AssignStmt, Scope; or nil
    5. Data interface{} // object-specific data; or nil
    6. Type interface{} // placeholder for type information; may be nil
    7. }

    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:

    1. Kind Data type Data value
    2. Pkg *Scope package scope

    func

    1. func NewObj(kind , name string) *

    NewObj creates a new object of a given kind and name.

    func (*Object) Pos

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

    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).

    type

    1. type Package struct {
    2. Name // package name
    3. Scope *Scope // package scope across all files
    4. Imports map[]*Object // map of package id -> package object
    5. Files map[]*File // Go source files by filename
    6. }

    A Package node represents a set of source files collectively building a Go
    package.

    func

    1. func NewPackage(fset *.FileSet, files map[]*File, importer , universe *Scope) (*, 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.

    func (*Package)

    1. func (p *) End() token.

    func (*Package) Pos

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

    type

    1. type ParenExpr struct {
    2. Lparen .Pos // position of "("
    3. X // parenthesized expression
    4. Rparen token. // position of ")"
    5. }

    A ParenExpr node represents a parenthesized expression.

    func (*ParenExpr) End

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

    func (*ParenExpr)

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

    type RangeStmt

    1. type RangeStmt struct {
    2. For token. // position of "for" keyword
    3. Key, Value Expr // Key, Value may be nil
    4. TokPos .Pos // position of Tok; invalid if Key == nil
    5. Tok .Token // ILLEGAL if Key == nil, ASSIGN, DEFINE
    6. X // value to range over
    7. Body *BlockStmt
    8. }

    A RangeStmt represents a for statement with a range clause.

    func (*RangeStmt)

    1. func (s *) End() token.

    func (*RangeStmt) Pos

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

    type

    1. type ReturnStmt struct {
    2. Return .Pos // position of "return" keyword
    3. Results [] // result expressions; or nil
    4. }

    A ReturnStmt node represents a return statement.

    func (*ReturnStmt) End

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

    func (*ReturnStmt)

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

    type Scope

    1. type Scope struct {
    2. Outer *Scope
    3. Objects map[]*Object
    4. }

    A Scope maintains the set of named language entities declared in the scope and a
    link to the immediately surrounding (outer) scope.

    func

    1. func NewScope(outer *) *Scope

    NewScope creates a new scope nested in the outer scope.

    func (*Scope)

    1. func (s *) Insert(obj *Object) (alt *)

    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.

    func (*Scope) Lookup

    1. func (s *Scope) Lookup(name ) *Object

    Lookup returns the object with the given name if it is found in scope s,
    otherwise it returns nil. Outer scopes are ignored.

    func (*Scope)

    1. func (s *) String() string

    Debugging support

    type

    1. type SelectStmt struct {
    2. Select .Pos // position of "select" keyword
    3. Body * // CommClauses only
    4. }

    An SelectStmt node represents a select statement.

    func (*SelectStmt) End

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

    func (*SelectStmt)

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

    type SelectorExpr

    1. type SelectorExpr struct {
    2. X Expr // expression
    3. Sel * // field selector
    4. }

    A SelectorExpr node represents an expression followed by a selector.

    func (*SelectorExpr) End

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

    func (*SelectorExpr)

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

    type SendStmt

    1. type SendStmt struct {
    2. Chan Expr
    3. Arrow .Pos // position of "<-"
    4. Value
    5. }

    A SendStmt node represents a send statement.

    func (*SendStmt) End

    1. func (s *SendStmt) End() .Pos

    func (*SendStmt)

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

    type SliceExpr

    1. type SliceExpr struct {
    2. X Expr // expression
    3. Lbrack .Pos // position of "["
    4. Low // begin of slice range; or nil
    5. High Expr // end of slice range; or nil
    6. Max // maximum capacity of slice; or nil
    7. Slice3 bool // true if 3-index slice (2 colons present)
    8. Rbrack .Pos // position of "]"
    9. }

    An SliceExpr node represents an expression followed by slice indices.

    func (*SliceExpr)

    1. func (x *) End() token.

    func (*SliceExpr) Pos

    1. func (x *SliceExpr) Pos() .Pos

    type

    1. type Spec interface {
    2. // contains filtered or unexported methods
    3. }

    The Spec type stands for any of ImportSpec, ValueSpec, and *TypeSpec.

    type StarExpr

    1. type StarExpr struct {
    2. Star token. // position of "*"
    3. X Expr // operand
    4. }

    A StarExpr node represents an expression of the form ““ Expression.
    Semantically it could be a unary “
    “ expression, or a pointer type.

    func (*StarExpr)

    1. func (x *) End() token.

    func (*StarExpr) Pos

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

    type

    1. type Stmt interface {
    2. // contains filtered or unexported methods
    3. }

    All statement nodes implement the Stmt interface.

    type StructType

    1. type StructType struct {
    2. Struct token. // position of "struct" keyword
    3. Fields *FieldList // list of field declarations
    4. Incomplete // true if (source) fields are missing in the Fields list
    5. }

    A StructType node represents a struct type.

    func (*StructType) End

    1. func (x *StructType) End() .Pos

    func (*StructType)

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

    type SwitchStmt

    1. type SwitchStmt struct {
    2. Switch token. // position of "switch" keyword
    3. Init Stmt // initialization statement; or nil
    4. Tag // tag expression; or nil
    5. Body *BlockStmt // CaseClauses only
    6. }

    A SwitchStmt node represents an expression switch statement.

    func (*SwitchStmt)

    1. func (s *) End() token.

    func (*SwitchStmt) Pos

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

    type

    1. type TypeAssertExpr struct {
    2. X // expression
    3. Lparen token. // position of "("
    4. Type Expr // asserted type; nil means type switch X.(type)
    5. Rparen .Pos // position of ")"
    6. }

    A TypeAssertExpr node represents an expression followed by a type assertion.

    func (*TypeAssertExpr)

    1. func (x *) End() token.

    func (*TypeAssertExpr) Pos

    1. func (x *TypeAssertExpr) Pos() .Pos

    type

    1. type TypeSpec struct {
    2. Doc * // associated documentation; or nil
    3. Name *Ident // type name
    4. Assign .Pos // position of '=', if any
    5. Type // *Ident, *ParenExpr, *SelectorExpr, *StarExpr, or any of the *XxxTypes
    6. Comment *CommentGroup // line comments; or nil
    7. }

    A TypeSpec node represents a type declaration (TypeSpec production).

    func (*TypeSpec)

    1. func (s *) End() token.

    func (*TypeSpec) Pos

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

    type

    1. type TypeSwitchStmt struct {
    2. Switch .Pos // position of "switch" keyword
    3. Init // initialization statement; or nil
    4. Assign Stmt // x := y.(type) or y.(type)
    5. Body * // CaseClauses only
    6. }

    An TypeSwitchStmt node represents a type switch statement.

    func (*TypeSwitchStmt) End

    1. func (s *TypeSwitchStmt) End() .Pos

    func (*TypeSwitchStmt)

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

    type UnaryExpr

    1. type UnaryExpr struct {
    2. OpPos token. // position of Op
    3. Op token. // operator
    4. X Expr // operand
    5. }

    A UnaryExpr node represents a unary expression. Unary “*” expressions are
    represented via StarExpr nodes.

    func (*UnaryExpr)

    1. func (x *) End() token.

    func (*UnaryExpr) Pos

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

    type

    1. type ValueSpec struct {
    2. Doc * // associated documentation; or nil
    3. Names []*Ident // value names (len(Names) > 0)
    4. Type // value type; or nil
    5. Values []Expr // initial values; or nil
    6. Comment * // line comments; or nil
    7. }

    A ValueSpec node represents a constant or variable declaration (ConstSpec or
    VarSpec production).

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

    func (*ValueSpec)

      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).