Standard library / go/printer
go/printer.Fprint()
Pretty-print a supported AST node using default settings; use format.Node for canonical gofmt output. Expected stdout: '1 + 2\n'.
Formal Syntax & Structure
go syntaxgo
func Fprint(output io.Writer, fset *token.FileSet, node any) errorParameters & Attributes
| Name | Type | Description |
|---|---|---|
| output | io.Writer | Destination writer for printed source. |
| fset | *token.FileSet | File set used to parse source. |
| node | any | Supported AST node or node list to print. |
Return Value
error
Parsing or setting error; nil on success.
Working Example
Example Snippetgo
package main
import ( "bytes" "fmt" "go/parser" "go/printer" "go/token")
func check(e error) { if e != nil { panic(e) }}
func main() { fs := token.NewFileSet() e, err := parser.ParseExprFrom(fs, "demo.go", "1+2", 0) check(err) var b bytes.Buffer check(printer.Fprint(&b, fs, e)) fmt.Println(b.String())}