format.go

v1.3.2
Doc Versions Source
1
package godoc
2
3
import (
4
	"bytes"
5
	"fmt"
6
	"go/ast"
7
	"go/format"
8
	"go/token"
9
)
10
11
// formatNode renders an AST node as formatted Go source.
12
func formatNode(fset *token.FileSet, node ast.Node) string {
13
	var buf bytes.Buffer
14
	if err := format.Node(&buf, fset, node); err != nil {
15
		return fmt.Sprintf("/* format error: %v */", err)
16
	}
17
	return buf.String()
18
}
19
20
// formatExpr renders an expression as Go source text.
21
func formatExpr(expr ast.Expr) string {
22
	var buf bytes.Buffer
23
	if err := format.Node(&buf, token.NewFileSet(), expr); err != nil {
24
		return "?"
25
	}
26
	return buf.String()
27
}
28

Source Files