1
0
mirror of https://github.com/golang/go synced 2024-11-22 20:50:05 -07:00

io/fs: document that caller can modify slice returned by ReadFile

Also add a test to testing/fstest.

Fixes #45186

Change-Id: I00e5f46ccd5269dbc266a8f2ebc9a62ebb1297b8
Reviewed-on: https://go-review.googlesource.com/c/go/+/311649
Trust: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Russ Cox <rsc@golang.org>
This commit is contained in:
Ian Lance Taylor 2021-04-19 16:05:01 -07:00
parent b8a359d984
commit 35806efda2
2 changed files with 15 additions and 0 deletions

View File

@ -15,6 +15,9 @@ type ReadFileFS interface {
// A successful call returns a nil error, not io.EOF.
// (Because ReadFile reads the whole file, the expected EOF
// from the final Read is not treated as an error to be reported.)
//
// The caller is permitted to modify the returned byte slice.
// This method should return a copy of the underlying data.
ReadFile(name string) ([]byte, error)
}

View File

@ -537,6 +537,18 @@ func (t *fsTester) checkFile(file string) {
}
t.checkFileRead(file, "ReadAll vs fsys.ReadFile", data, data2)
// Modify the data and check it again. Modifying the
// returned byte slice should not affect the next call.
for i := range data2 {
data2[i]++
}
data2, err = fsys.ReadFile(file)
if err != nil {
t.errorf("%s: second call to fsys.ReadFile: %v", file, err)
return
}
t.checkFileRead(file, "Readall vs second fsys.ReadFile", data, data2)
t.checkBadPath(file, "ReadFile",
func(name string) error { _, err := fsys.ReadFile(name); return err })
}