Skip to content

Commit d6b9994

Browse files
mvdanjosharian
authored andcommitted
go/parser: fix example to run on the playground
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í <[email protected]> Reviewed-by: Robert Griesemer <[email protected]>
1 parent 26308fb commit d6b9994

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

src/go/parser/example_test.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,19 @@ import (
1313
func ExampleParseFile() {
1414
fset := token.NewFileSet() // positions are relative to fset
1515

16-
// Parse the file containing this very example
17-
// but stop after processing the imports.
18-
f, err := parser.ParseFile(fset, "example_test.go", nil, parser.ImportsOnly)
16+
src := `package foo
17+
18+
import (
19+
"fmt"
20+
"time"
21+
)
22+
23+
func bar() {
24+
fmt.Println(time.Now())
25+
}`
26+
27+
// Parse src but stop after processing the imports.
28+
f, err := parser.ParseFile(fset, "", src, parser.ImportsOnly)
1929
if err != nil {
2030
fmt.Println(err)
2131
return
@@ -29,6 +39,5 @@ func ExampleParseFile() {
2939
// output:
3040
//
3141
// "fmt"
32-
// "go/parser"
33-
// "go/token"
42+
// "time"
3443
}

0 commit comments

Comments
 (0)