Standard library / go/format
go/format.Node()
Write supported AST nodes in canonical gofmt style without modifying them. Use the original file set. Partial source does not receive import sorting. Expected stdout: '1 + 2*3\n'.
Formal Syntax & Structure
go syntaxgo
func Node(dst io.Writer, fset *token.FileSet, node any) errorParameters & Attributes
| Name | Type | Description |
|---|---|---|
| dst | io.Writer | Destination writer; may return an error. |
| 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/format" "go/parser" "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*3", 0) check(err) var b bytes.Buffer check(format.Node(&b, fs, e)) fmt.Println(b.String())}