...

Source file src/go/format/example_test.go

Documentation: go/format

		 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  package format_test
		 6  
		 7  import (
		 8  	"bytes"
		 9  	"fmt"
		10  	"go/format"
		11  	"go/parser"
		12  	"go/token"
		13  	"log"
		14  )
		15  
		16  func ExampleNode() {
		17  	const expr = "(6+2*3)/4"
		18  
		19  	// parser.ParseExpr parses the argument and returns the
		20  	// corresponding ast.Node.
		21  	node, err := parser.ParseExpr(expr)
		22  	if err != nil {
		23  		log.Fatal(err)
		24  	}
		25  
		26  	// Create a FileSet for node. Since the node does not come
		27  	// from a real source file, fset will be empty.
		28  	fset := token.NewFileSet()
		29  
		30  	var buf bytes.Buffer
		31  	err = format.Node(&buf, fset, node)
		32  	if err != nil {
		33  		log.Fatal(err)
		34  	}
		35  
		36  	fmt.Println(buf.String())
		37  
		38  	// Output: (6 + 2*3) / 4
		39  }
		40  

View as plain text