libgo: update to Go 1.11
[official-gcc.git] / libgo / go / go / format / example_test.go
blob3d72997fa201d647f9c77add0eee1bff3c7ef5c3
1 // Copyright 2018 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // +build gccgo_examples
7 package format_test
9 import (
10 "bytes"
11 "fmt"
12 "go/format"
13 "go/parser"
14 "go/token"
15 "log"
18 func ExampleNode() {
19 const expr = "(6+2*3)/4"
21 // parser.ParseExpr parses the argument and returns the
22 // corresponding ast.Node.
23 node, err := parser.ParseExpr(expr)
24 if err != nil {
25 log.Fatal(err)
28 // Create a FileSet for node. Since the node does not come
29 // from a real source file, fset will be empty.
30 fset := token.NewFileSet()
32 var buf bytes.Buffer
33 err = format.Node(&buf, fset, node)
34 if err != nil {
35 log.Fatal(err)
38 fmt.Println(buf.String())
40 // Output: (6 + 2*3) / 4