diff options
| author | Alberto Donizetti <alb.donizetti@gmail.com> | 2016-08-10 19:01:06 +0200 |
|---|---|---|
| committer | Brad Fitzpatrick <bradfitz@golang.org> | 2016-09-08 04:12:35 +0000 |
| commit | b6f44923c0f88eb36816d90fb8fff2fd78422df5 (patch) | |
| tree | 383edbb5829db36fb53f8f0a99666b8cc17fe227 /src | |
| parent | 614dfe9b02d69f96f4b222d818ec5ff47f26cb31 (diff) | |
| download | go-b6f44923c0f88eb36816d90fb8fff2fd78422df5.tar.xz | |
go/format: add format.Node example
Updates #16360
Change-Id: I5927cffa961cd85539a3ba9606b116c5996d1096
Reviewed-on: https://go-review.googlesource.com/26696
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
Diffstat (limited to 'src')
| -rw-r--r-- | src/go/format/format_test.go | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/src/go/format/format_test.go b/src/go/format/format_test.go index b5817a5dd1..72b8d5aeeb 100644 --- a/src/go/format/format_test.go +++ b/src/go/format/format_test.go @@ -6,9 +6,11 @@ package format import ( "bytes" + "fmt" "go/parser" "go/token" "io/ioutil" + "log" "strings" "testing" ) @@ -143,3 +145,28 @@ func TestPartial(t *testing.T) { } } } + +func ExampleNode() { + const expr = "(6+2*3)/4" + + // parser.ParseExpr parses the argument and returns the + // corresponding ast.Node. + node, err := parser.ParseExpr(expr) + if err != nil { + log.Fatal(err) + } + + // Create a FileSet for node. Since the node does not come + // from a real source file, fset will be empty. + fset := token.NewFileSet() + + var buf bytes.Buffer + err = Node(&buf, fset, node) + if err != nil { + log.Fatal(err) + } + + fmt.Println(buf.String()) + + // Output: (6 + 2*3) / 4 +} |
