Skip to content

Instantly share code, notes, and snippets.

@mh-cbon
Created January 17, 2017 17:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mh-cbon/3ed5d9c39e9635cfed0f896000098133 to your computer and use it in GitHub Desktop.
Save mh-cbon/3ed5d9c39e9635cfed0f896000098133 to your computer and use it in GitHub Desktop.
print go ast node without nil, nor ast.Object
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