]> Cypherpunks repositories - gostls13.git/commitdiff
go/parser: fix example to run on the playground
authorDaniel Martí <mvdan@mvdan.cc>
Mon, 3 Apr 2017 14:18:30 +0000 (15:18 +0100)
committerJosh Bleecher Snyder <josharian@gmail.com>
Mon, 3 Apr 2017 16:07:09 +0000 (16:07 +0000)
The example shouldn't rely on the existance of example_test.go. That
breaks in the playground, which is what the "run" button in
https://golang.org/pkg/go/parser/#example_ParseFile does.

Make the example self-sufficient by using a small piece of source via a
string literal instead.

Fixes #19823.

Change-Id: Ie8a3c6c5d00724e38ff727862b62e6a3621adc88
Reviewed-on: https://go-review.googlesource.com/39236
Run-TryBot: Daniel Martí <mvdan@mvdan.cc>
Reviewed-by: Robert Griesemer <gri@golang.org>
src/go/parser/example_test.go

index 3c58e63a99e532bfee982df93323ebbcc56ba496..c2f7f293bc14679f49937b4a32c0fe33d742af2c 100644 (file)
@@ -13,9 +13,19 @@ import (
 func ExampleParseFile() {
        fset := token.NewFileSet() // positions are relative to fset
 
-       // Parse the file containing this very example
-       // but stop after processing the imports.
-       f, err := parser.ParseFile(fset, "example_test.go", nil, parser.ImportsOnly)
+       src := `package foo
+
+import (
+       "fmt"
+       "time"
+)
+
+func bar() {
+       fmt.Println(time.Now())
+}`
+
+       // Parse src but stop after processing the imports.
+       f, err := parser.ParseFile(fset, "", src, parser.ImportsOnly)
        if err != nil {
                fmt.Println(err)
                return
@@ -29,6 +39,5 @@ func ExampleParseFile() {
        // output:
        //
        // "fmt"
-       // "go/parser"
-       // "go/token"
+       // "time"
 }