From 8c3924c6569ceb8183c136e42c9f4b7904a0c031 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Daniel=20Mart=C3=AD?= Date: Sun, 24 Sep 2023 14:41:10 +0100 Subject: [PATCH] path/filepath: reuse os.ReadDir While reading the source code, I noticed that readDir seemed extremely similar to os.ReadDir. They indeed appear to be copies. Note that there's readDirNames as well, but it has no corresponding os.ReadDirNames top-level helper to be replaced by. Change-Id: I6fe1d0aeda35dc69bb4531986fe3a21ebda1d877 Reviewed-on: https://go-review.googlesource.com/c/go/+/530795 Reviewed-by: Than McIntosh Reviewed-by: Ian Lance Taylor LUCI-TryBot-Result: Go LUCI --- src/path/filepath/path.go | 18 +----------------- 1 file changed, 1 insertion(+), 17 deletions(-) diff --git a/src/path/filepath/path.go b/src/path/filepath/path.go index 6dcb0e1fb9..b1f1bf0e3f 100644 --- a/src/path/filepath/path.go +++ b/src/path/filepath/path.go @@ -463,7 +463,7 @@ func walkDir(path string, d fs.DirEntry, walkDirFn fs.WalkDirFunc) error { return err } - dirs, err := readDir(path) + dirs, err := os.ReadDir(path) if err != nil { // Second call, to report ReadDir error. err = walkDirFn(path, d, err) @@ -580,22 +580,6 @@ func Walk(root string, fn WalkFunc) error { return err } -// readDir reads the directory named by dirname and returns -// a sorted list of directory entries. -func readDir(dirname string) ([]fs.DirEntry, error) { - f, err := os.Open(dirname) - if err != nil { - return nil, err - } - dirs, err := f.ReadDir(-1) - f.Close() - if err != nil { - return nil, err - } - sort.Slice(dirs, func(i, j int) bool { return dirs[i].Name() < dirs[j].Name() }) - return dirs, nil -} - // readDirNames reads the directory named by dirname and returns // a sorted list of directory entry names. func readDirNames(dirname string) ([]string, error) { -- 2.50.0