Commit d6b99943 authored by Daniel Martí's avatar Daniel Martí Committed by Josh Bleecher Snyder

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í <mvdan@mvdan.cc>
Reviewed-by: default avatarRobert Griesemer <gri@golang.org>
parent 26308fb4
...@@ -13,9 +13,19 @@ import ( ...@@ -13,9 +13,19 @@ import (
func ExampleParseFile() { func ExampleParseFile() {
fset := token.NewFileSet() // positions are relative to fset fset := token.NewFileSet() // positions are relative to fset
// Parse the file containing this very example src := `package foo
// but stop after processing the imports.
f, err := parser.ParseFile(fset, "example_test.go", nil, parser.ImportsOnly) 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 { if err != nil {
fmt.Println(err) fmt.Println(err)
return return
...@@ -29,6 +39,5 @@ func ExampleParseFile() { ...@@ -29,6 +39,5 @@ func ExampleParseFile() {
// output: // output:
// //
// "fmt" // "fmt"
// "go/parser" // "time"
// "go/token"
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment