Standard library / go/ast
go/ast.Preorder()
Returns a depth-first preorder iterator over non-nil AST nodes. Stop iteration with break when no more nodes are needed. Requires Go 1.23. Expected stdout: '*ast.BinaryExpr\n*ast.BasicLit\n*ast.BasicLit\n'.
Formal Syntax & Structure
go syntaxgo
func Preorder(root Node) iter.Seq[Node]Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| root | Node | Root syntax node of the traversal. |
Return Value
iter.Seq[Node]
Returns a depth-first preorder iterator over non-nil AST nodes.
Working Example
Example Snippetgo
package main
import ( "fmt" "go/ast" "go/parser")
func check(e error) { if e != nil { panic(e) }}
func main() { x, e := parser.ParseExpr("1+2") check(e) for n := range ast.Preorder(x) { fmt.Printf("%T\n", n) }}