package main import ( "go/parser" "go/token" "github.com/dave/dst" "github.com/dave/dst/decorator" "github.com/dave/dst/dstutil" ) // Utility error checking function for when you don't need to gracefully handle errors func must(err error) { if err != nil { panic(err) } } func main() { file, err := decorator.Parse(srcCodeString) must(err) // Notice that we have to define our own function examining/editting a node during AST traversal applyFunc := func(c *dstutil.Cursor) bool { node := c.Node() // Use a switch-case construct based on the node "type" // This is a very useful of navigating the AST switch n := node.(type) { case (*dst.FuncDecl): // Pretty print the Node AST dst.Print(n) } return true } // We traverse the Go AST via the Apply function // If the node is "nil" or the return value is "false" traversal stops // Lastly, it's possible to edit the AST while doing the traversal and return the result _ = dstutil.Apply(file, applyFunc, nil) }