Skip to content

Commit b6f4492

Browse files
ALTreebradfitz
authored andcommitted
go/format: add format.Node example
Updates #16360 Change-Id: I5927cffa961cd85539a3ba9606b116c5996d1096 Reviewed-on: https://go-review.googlesource.com/26696 Reviewed-by: Brad Fitzpatrick <[email protected]> Run-TryBot: Brad Fitzpatrick <[email protected]>
1 parent 614dfe9 commit b6f4492

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

src/go/format/format_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ package format
66

77
import (
88
"bytes"
9+
"fmt"
910
"go/parser"
1011
"go/token"
1112
"io/ioutil"
13+
"log"
1214
"strings"
1315
"testing"
1416
)
@@ -143,3 +145,28 @@ func TestPartial(t *testing.T) {
143145
}
144146
}
145147
}
148+
149+
func ExampleNode() {
150+
const expr = "(6+2*3)/4"
151+
152+
// parser.ParseExpr parses the argument and returns the
153+
// corresponding ast.Node.
154+
node, err := parser.ParseExpr(expr)
155+
if err != nil {
156+
log.Fatal(err)
157+
}
158+
159+
// Create a FileSet for node. Since the node does not come
160+
// from a real source file, fset will be empty.
161+
fset := token.NewFileSet()
162+
163+
var buf bytes.Buffer
164+
err = Node(&buf, fset, node)
165+
if err != nil {
166+
log.Fatal(err)
167+
}
168+
169+
fmt.Println(buf.String())
170+
171+
// Output: (6 + 2*3) / 4
172+
}

0 commit comments

Comments
 (0)