Golang go/printer.Config.Fprint() function example

package go/printer

Fprint "pretty-prints" an AST node to output for a given configuration cfg. Position information is interpreted relative to the file set fset. The node type must be *ast.File, *CommentedNode, []ast.Decl, []ast.Stmt, or assignment-compatible to ast.Expr, ast.Decl, ast.Spec, or ast.Stmt.

Golang go/printer.Config.Fprint() function usage example

 func Process(filename string, src []byte, opt *Options) ([]byte, error) {
 if opt == nil {
 opt = &Options{Comments: true, TabIndent: true, TabWidth: 8}
 }
 fileSet := token.NewFileSet()
 file, adjust, err := parse(fileSet, filename, src, opt)
 if err != nil {
 return nil, err
 }
 ...
 var buf bytes.Buffer
 err = printConfig.Fprint(&buf, fileSet, file) // <-- here
 if err != nil {
 return nil, err
 }
 ...

References :

https://code.google.com/p/go/source/browse/go/importer/import.go?repo=tools

http://golang.org/pkg/go/printer/#Config.Fprint

Advertisement