mirror of
https://github.com/golang/go
synced 2024-11-18 16:14:46 -07:00
go/format: move example to external test file
Per #11257 all examples should be in external test files. Additionally, doing so makes this example playable. Updates #24352. (Albeit tangentially). Change-Id: I77ab4655107f61db2e9d21a608b73ace3a230fb2 Reviewed-on: https://go-review.googlesource.com/101285 Reviewed-by: Robert Griesemer <gri@golang.org>
This commit is contained in:
parent
5c3cb64b2e
commit
1101a902fe
39
src/go/format/example_test.go
Normal file
39
src/go/format/example_test.go
Normal file
@ -0,0 +1,39 @@
|
||||
// Copyright 2018 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package format_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"go/format"
|
||||
"go/parser"
|
||||
"go/token"
|
||||
"log"
|
||||
)
|
||||
|
||||
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 = format.Node(&buf, fset, node)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
fmt.Println(buf.String())
|
||||
|
||||
// Output: (6 + 2*3) / 4
|
||||
}
|
@ -6,11 +6,9 @@ package format
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"go/parser"
|
||||
"go/token"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
@ -145,28 +143,3 @@ 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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user