Golang go/ast.NewIdent() function example

package go/ast

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

Golang go/ast.NewIdent() function usage example

 var varVar  = flag.String("var", "GoCover", "name of coverage variable to generate")

 func (f *File) newCounter(start, end token.Pos, numStmt int) ast.Stmt {
  counter := &ast.IndexExpr{
 X: &ast.SelectorExpr{
 X: ast.NewIdent(*varVar), // <-- here
 Sel: ast.NewIdent("Count"),
 },
 Index: f.index(),
  }
  stmt := counterStmt(f, counter)
  f.blocks = append(f.blocks, Block{start, end, numStmt})
  return stmt
 }

References :

https://github.com/lattera/go.tools/blob/master/cmd/cover/cover.go

http://golang.org/pkg/go/ast/#NewIdent

Advertisement