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 returns "No such file or directory" because testdata does not exist #32916

Open
KolesnikDmitriy opened this issue Jul 3, 2019 · 8 comments

Comments

@KolesnikDmitriy
Copy link

commented Jul 3, 2019

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

$ go version
go1.12.6

Does this issue reproduce with the latest release?

Yes

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

go env Output
$ go env
https://play.golang.org/

What did you do?

Run example in https://golang.org/pkg/io/ioutil/#WriteFile

What did you expect to see?

Expected file created

What did you see instead?

2009/11/10 23:00:00 open testdata/hello: No such file or directory

Program exited.

@AlexRouSg

This comment has been minimized.

Copy link
Contributor

commented Jul 3, 2019

Are you running the example in the playground or on your system?

EDIT: Sorry was looking at the wrong example

@av86743

This comment has been minimized.

Copy link

commented Jul 3, 2019

Indeed, that link https://golang.org/pkg/io/ioutil/#WriteFile above, which offers much better func TempFile choice, also with examples, deserves an issue on its own.

@agnivade

This comment has been minimized.

Copy link
Member

commented Jul 3, 2019

Thanks. The code in the example assumes a testdata directory. Since it cannot find it, it throws an error. It worked so far because the source tree always had a testdata directory.

You are welcome to send a patch.

@agnivade agnivade changed the title ioutil.WriteFile return "No such file or directory" in example io/ioutil: WriteFile example returns "No such file or directory" because testdata does not exist Jul 3, 2019

@agnivade agnivade added this to the Unplanned milestone Jul 3, 2019

@darkLord19

This comment has been minimized.

Copy link
Contributor

commented Jul 4, 2019

@cuonglm

This comment has been minimized.

Copy link
Contributor

commented Jul 4, 2019

@darkLord19 Examples are run in separate environment, without the directory structure as in source code. ReadFile has the same problem as it read testdata/hello file.

@gopherbot

This comment has been minimized.

Copy link

commented Jul 4, 2019

Change https://golang.org/cl/185037 mentions this issue: io/ioutil: make Read/WriteFile examples runnable in playground

@av86743

This comment has been minimized.

Copy link

commented Jul 9, 2019

Regarding https://golang.org/cl/185037 :

$ find go/src/github.com/av86743/go/src -type f -name 'example_test.go' -exec grep -e '"testdata' {} ';' -print 
	r, err := zip.OpenReader("testdata/readme.zip")
go/src/github.com/av86743/go/src/archive/zip/example_test.go
	cert, err := tls.LoadX509KeyPair("testdata/example-cert.pem", "testdata/example-key.pem")
go/src/github.com/av86743/go/src/crypto/tls/example_test.go
	content, err := ioutil.ReadFile("testdata/hello")
	err := ioutil.WriteFile("testdata/hello", message, 0644)
go/src/github.com/av86743/go/src/io/ioutil/example_test.go
$
@bcmills

This comment has been minimized.

Copy link
Member

commented Jul 15, 2019

I believe this is a specific case of the broader issue #19825.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
8 participants
You can’t perform that action at this time.