diff options
author | Daniel Martà <mvdan@mvdan.cc> | 2022-04-12 07:11:28 +0100 |
---|---|---|
committer | Gopher Robot <gobot@golang.org> | 2022-04-19 16:19:27 +0000 |
commit | e04280838f34669458d55d6aab812a8c304c40a0 (patch) | |
tree | ce573018844d132a314853cc5bc4797100078cac /src/io | |
parent | e25f46e59621caf02f8ac1acc8d1dbe028859e47 (diff) | |
download | go-e04280838f34669458d55d6aab812a8c304c40a0.tar.gz go-e04280838f34669458d55d6aab812a8c304c40a0.zip |
io/ioutil: provide an equivalent for the deprecated ReadDir
All APIs in the now-deprecated io/ioutil package have a direct
replacement in either the io or os package with the same signature,
with the notable exception of ioutil.ReadDir, as os.ReadDir has a
slightly different signature with fs.DirEntry rather than fs.FileInfo.
New code can easily make use of []fs.DirEntry directly,
but existing code may need to continue using []fs.FileInfo for backwards
compatibility reasons. For instance, I had a bit of code that exposed
the slice as a public API, like:
return ioutil.ReadDir(name)
It took me a couple of minutes to figure out what the exact equivalent
in terms of os.ReadDir would be, and a code sample would have helped.
Add one for future reference.
For #42026.
For #51927.
Change-Id: I76d46cd7d68fc609c873821755fdcfc299ffd56c
Reviewed-on: https://go-review.googlesource.com/c/go/+/399854
Reviewed-by: Bryan Mills <bcmills@google.com>
Run-TryBot: Bryan Mills <bcmills@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Run-TryBot: Ian Lance Taylor <iant@google.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
Diffstat (limited to 'src/io')
-rw-r--r-- | src/io/ioutil/ioutil.go | 11 |
1 files changed, 11 insertions, 0 deletions
diff --git a/src/io/ioutil/ioutil.go b/src/io/ioutil/ioutil.go index 9921c2ae50..6a1d69172c 100644 --- a/src/io/ioutil/ioutil.go +++ b/src/io/ioutil/ioutil.go @@ -55,6 +55,17 @@ func WriteFile(filename string, data []byte, perm fs.FileMode) error { // it returns a list of fs.DirEntry instead of fs.FileInfo, // and it returns partial results in the case of an error // midway through reading a directory. +// +// If you must continue obtaining a list of fs.FileInfo, you still can: +// +// entries, err := os.ReadDir(dirname) +// if err != nil { ... } +// infos := make([]fs.FileInfo, 0, len(entries)) +// for _, entry := range entries { +// info, err := entry.Info() +// if err != nil { ... } +// infos = append(infos, info) +// } func ReadDir(dirname string) ([]fs.FileInfo, error) { f, err := os.Open(dirname) if err != nil { |