Standard library / go/ast

go/ast.Visitor

Defines the callback contract for Walk; a nil returned visitor prunes a subtree. Expected stdout: '1\n'.

Formal Syntax & Structure

go syntaxgo
type Visitor interface {  Visit(node Node) (w Visitor)}

Working Example

Example Snippetgo
package main
import (  "fmt"  "go/ast")
type counter struct{ n *int }
func (v counter) Visit(n ast.Node) ast.Visitor {  if n != nil {    *v.n++    return v  }  return nil}
func main() {  x := ast.NewIdent("x")  n := 0  var v ast.Visitor = counter{&n}  ast.Walk(v, x)  fmt.Println(n)}
Go Reference