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) errorParameters & Attributes
| Name | Type | Description |
|---|---|---|
| w | io.Writer | Destination writer. |
| fset | *token.FileSet | Non-nil file set that records source positions. |
| x | any | Syntax tree or subtree to print. |
| f | FieldFilter | Predicate 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"))}