Skip to content

Commit e042808

Browse files
mvdangopherbot
authored andcommitted
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 <[email protected]> Run-TryBot: Bryan Mills <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Run-TryBot: Ian Lance Taylor <[email protected]> Auto-Submit: Ian Lance Taylor <[email protected]>
1 parent e25f46e commit e042808

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

src/io/ioutil/ioutil.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,17 @@ func WriteFile(filename string, data []byte, perm fs.FileMode) error {
5555
// it returns a list of fs.DirEntry instead of fs.FileInfo,
5656
// and it returns partial results in the case of an error
5757
// midway through reading a directory.
58+
//
59+
// If you must continue obtaining a list of fs.FileInfo, you still can:
60+
//
61+
// entries, err := os.ReadDir(dirname)
62+
// if err != nil { ... }
63+
// infos := make([]fs.FileInfo, 0, len(entries))
64+
// for _, entry := range entries {
65+
// info, err := entry.Info()
66+
// if err != nil { ... }
67+
// infos = append(infos, info)
68+
// }
5869
func ReadDir(dirname string) ([]fs.FileInfo, error) {
5970
f, err := os.Open(dirname)
6071
if err != nil {

0 commit comments

Comments
 (0)