Golang go/ast.Decl type examples

package go/ast

All declaration nodes implement the Decl interface.

Golang go/ast.Decl type usage examples

Example 1:

 func ParseDecl(x string) (ast.Decl, error) {
 file, err := parser.ParseFile(token.NewFileSet(), "", "package p\n//line :1\n"+x+"\n", 0)
 if err != nil {
 return nil, err
 }
 return file.Decls[0], nil
 }

Example 2:

 func (check *Checker) declStmt(decl ast.Decl) {
 pkg := check.pkg

 switch d := decl.(type) {
 case *ast.BadDecl:
 // ignore

 case *ast.GenDecl:
 // do something
 ...
 }

References :

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

https://github.com/shurcooL/go/blob/master/gists/gist5707298/main.go

Advertisement