Created
January 17, 2017 17:26
-
-
Save mh-cbon/3ed5d9c39e9635cfed0f896000098133 to your computer and use it in GitHub Desktop.
print go ast node without nil, nor ast.Object
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
package main | |
import ( | |
"go/ast" | |
"go/parser" | |
"go/token" | |
"os" | |
"reflect" | |
) | |
func main() { | |
// src is the input for which we want to print the AST. | |
src := ` | |
package main | |
func main() { | |
v := []string{} | |
for _, x := range v { | |
} | |
} | |
` | |
// Create the AST by parsing src. | |
fset := token.NewFileSet() // positions are relative to fset | |
f, err := parser.ParseFile(fset, "", src, 0) | |
if err != nil { | |
panic(err) | |
} | |
// Print the AST. | |
// ast.Print(fset, f) | |
ast.Fprint(os.Stdout, fset, f, func(name string, value reflect.Value) bool { | |
if ast.NotNilFilter(name, value) { | |
return value.Type().String() != "*ast.Object" | |
} | |
return false | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment