Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Walk ¶
Walk traverses an AST in depth-first order: It starts by calling fn(node); node must not be nil. It returns the rewritten node. If fn returns true, Walk invokes fn recursively for each of the non-nil children of node, followed by a call of fn(nil). The returned node of fn can be used to rewrite the passed node to fn. Panics if the returned type is not the same type as the original one.
Example ¶
src := `package main type Foo struct{}` fset := token.NewFileSet() file, err := parser.ParseFile(fset, "foo.go", src, parser.ParseComments) if err != nil { panic(err) } rewriteFunc := func(n ast.Node) (ast.Node, bool) { x, ok := n.(*ast.TypeSpec) if !ok { return n, true } // change struct type name to "Bar" x.Name.Name = "Bar" return x, true } rewritten := Walk(file, rewriteFunc) var buf bytes.Buffer printer.Fprint(&buf, fset, rewritten) fmt.Println(buf.String())
Output: package main type Bar struct{}
Types ¶
Click to show internal directories.
Click to hide internal directories.