Standard library / go/ast

go/ast.Fprint()

Prints an AST diagnostic representation to a writer; optional field filter controls displayed fields. Output format is for inspection, not stable serialization. Expected stdout: 'true\n'.

Formal Syntax & Structure

go syntaxgo
func Fprint(w io.Writer, fset *token.FileSet, x any, f FieldFilter) error

Parameters & Attributes

NameTypeDescription
wio.WriterDestination writer.
fset*token.FileSetNon-nil file set that records source positions.
xanySyntax tree or subtree to print.
fFieldFilterPredicate or callback controlling traversal, filtering or printing, as described above.

Return Value

error

Parsing or setting error; nil on success.

Working Example

Example Snippetgo
package main
import (  "bytes"  "fmt"  "go/ast"  "strings")
func check(e error) {  if e != nil {    panic(e)  }}
func main() {  var b bytes.Buffer  check(ast.Fprint(&b, nil, ast.NewIdent("Total"), ast.NotNilFilter))  fmt.Println(strings.Contains(b.String(), "Total"))}
Go Reference