Skip to content

Instantly share code, notes, and snippets.

@aaronlifton3
Created November 3, 2012 14:36
Show Gist options
  • Save aaronlifton3/4007541 to your computer and use it in GitHub Desktop.
Save aaronlifton3/4007541 to your computer and use it in GitHub Desktop.
golang ast insanity
funcAST, fset := parseFunc("../gooo.go", "main")
fs := token.NewFileSet()
node, _ := parser.ParseFile(fs, "../gooo.go", nil, 0)
for _, decl := range node.Decls {
if genDecl, ok := decl.(*ast.GenDecl); ok {
if genDecl.Tok == token.VAR {
for _, s := range genDecl.Specs {
vSpec := s.(*ast.ValueSpec)
for _, v := range vSpec.Names {
//fmt.Printf("%T", v.Pos()) // token.Pos
//fmt.Printf("%d: %s\n", v.Pos(), v.Name)
fmt.Printf("%s: %s\n", "Pos", v.Pos())
fmt.Printf("%s: %s\n", "Name", v.Name)
fmt.Printf("%s: %s\n", "Decl", v.Obj.Decl)
fmt.Printf("%s: %s\n", "Exported", strconv.FormatBool(v.IsExported()))
fmt.Println()
}
}
}
}
}
// OUTPUTS:
Pos: %!s(token.Pos=4363)
Name: templateCache
Decl: &{%!s(*ast.CommentGroup=<nil>) [templateCache] <nil> [false] %!s(*ast.CommentGroup=<nil>)}
Exported: false
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment