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

mntlty opened this issue Aug 6, 2020 · 4 comments

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

mntlty opened this issue Aug 6, 2020 · 4 comments


Copy link

@mntlty mntlty commented Aug 6, 2020

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


Does this issue reproduce with the latest release?


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

Go Playground

What did you do?

run example at which links to

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
Copy link

@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.

Copy link

@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
Copy link

@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.

Copy link

@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
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
5 participants