]> Cypherpunks repositories - gostls13.git/commitdiff
go/format: add format.Node example
authorAlberto Donizetti <alb.donizetti@gmail.com>
Wed, 10 Aug 2016 17:01:06 +0000 (19:01 +0200)
committerBrad Fitzpatrick <bradfitz@golang.org>
Thu, 8 Sep 2016 04:12:35 +0000 (04:12 +0000)
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>

src/go/format/format_test.go

index b5817a5dd183ab4b79191f6aca4ab38f27ddf3f1..72b8d5aeeb583c3d2a06f15f38d3ebff09bd897d 100644 (file)
@@ -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
+}