1
0
mirror of https://github.com/golang/go synced 2024-11-18 09:44:50 -07:00

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>
This commit is contained in:
Alberto Donizetti 2016-08-10 19:01:06 +02:00 committed by Brad Fitzpatrick
parent 614dfe9b02
commit b6f44923c0

View File

@ -6,9 +6,11 @@ package format
import ( import (
"bytes" "bytes"
"fmt"
"go/parser" "go/parser"
"go/token" "go/token"
"io/ioutil" "io/ioutil"
"log"
"strings" "strings"
"testing" "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
}