Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

io/ioutil: WriteFile example errors when run on the Go Playground #40623

Open
mntlty opened this issue Aug 6, 2020 · 4 comments
Open

io/ioutil: WriteFile example errors when run on the Go Playground #40623

mntlty opened this issue Aug 6, 2020 · 4 comments

Comments

@mntlty
Copy link

@mntlty mntlty commented Aug 6, 2020

What version of Go are you using (go version)?

latest

Does this issue reproduce with the latest release?

yes

What operating system and processor architecture are you using (go env)?

Go Playground https://play.golang.org/

What did you do?

run example at https://golang.org/pkg/io/ioutil/#WriteFile which links to https://play.golang.org/p/bIP2HMSXz1u

What did you expect to see?

no error

What did you see instead?

error: `open testdata/hello: no such file or directory

Suggested Fixes

WriteFile does not create a directory, and the code example tries to write to the non-existent directory testdata. Possible solution are either to create this directory in the example code or change the argument to WriteFile to just include the file name

@gopherbot gopherbot added this to the Unreleased milestone Aug 6, 2020
@tpaschalis
Copy link
Contributor

@tpaschalis tpaschalis commented Aug 6, 2020

You're right! In the background, ioutil.WriteFile uses os.OpenFile, and has been doing so since 2011, which doesn't create any intermediate directories.

I think that we could indeed change the example.
My 2¢ would be to add a couple of lines that create a directory in the example. We could also avoid the testdata name since it holds a special meaning in Go.

I'm not sure about documenting this behavior though since we don't explicitly mention anything about directories there.

@toothrot
Copy link
Contributor

@toothrot toothrot commented Aug 7, 2020

/cc @dmitshur

@dmitshur dmitshur changed the title x/website: ioutil.WriteFile example is broken io/ioutil: WriteFile example is broken Aug 7, 2020
@dmitshur dmitshur modified the milestones: Unreleased, Backlog Aug 7, 2020
@dmitshur dmitshur changed the title io/ioutil: WriteFile example is broken io/ioutil: WriteFile example errors when run on the Go Playground Aug 7, 2020
@dmitshur
Copy link
Contributor

@dmitshur dmitshur commented Aug 7, 2020

The testdata directory exists on disk. However, the example doesn’t have an // Output comment, so it’s only meant to be compiled and displayed in documentation, but not executed:

$ go test -v -run ExampleWriteFile io/ioutil
testing: warning: no tests to run

It doesn’t work on the Go Playground because the testdata directory isn’t available in that environment. But given there isn’t an // Output comment, maybe there shouldn’t be a button to run it on the website.

@mntlty
Copy link
Author

@mntlty mntlty commented Aug 7, 2020

@dmitshur the issue isn't really about the playground but that the example as written doesn't work without other steps that are not included here, like "must have this directory before running this code"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
5 participants