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. 4 5// +build gccgo_examples 6 7package format_test 8 9import ( 10 "bytes" 11 "fmt" 12 "go/format" 13 "go/parser" 14 "go/token" 15 "log" 16) 17 18func ExampleNode() { 19 const expr = "(6+2*3)/4" 20 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) 26 } 27 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() 31 32 var buf bytes.Buffer 33 err = format.Node(&buf, fset, node) 34 if err != nil { 35 log.Fatal(err) 36 } 37 38 fmt.Println(buf.String()) 39 40 // Output: (6 + 2*3) / 4 41} 42