Golang go/format.Node() function example

package go/format

Node formats node in canonical gofmt style and writes the result to dst (1st parameter).

The node type must be *ast.File, *printer.CommentedNode, []ast.Decl, []ast.Stmt, or assignment-compatible to ast.Expr, ast.Decl, ast.Spec, or ast.Stmt. Node does not modify node. Imports are not sorted for nodes representing partial source files (i.e., if the node is not an *ast.File or a *printer.CommentedNode not wrapping an *ast.File).

The function may return early (before the entire result is written) and return a formatting error, for instance due to an incorrect AST.

Golang go/format.Node() function usage example

 var fset *token.FileSet
 var f *ast.File,
 var buf bytes.Buffer

 if err := format.Node(&buf, fset, f); err != nil {
 fmt.Printf("Error formatting ast.File node: %v", err)
 os.Exit(1)
 }

Reference :

http://golang.org/pkg/go/format/#Node

Advertisement