Created
November 3, 2012 14:36
-
-
Save aaronlifton3/4007541 to your computer and use it in GitHub Desktop.
golang ast insanity
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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