aboutsummaryrefslogtreecommitdiff
path: root/src/os/dir.go
diff options
context:
space:
mode:
authorRuss Cox <rsc@golang.org>2020-10-29 13:51:20 -0400
committerRuss Cox <rsc@golang.org>2020-12-02 17:00:06 +0000
commit3d913a926675d8d6fcdc3cfaefd3136dfeba06e1 (patch)
treef67a6801ab129669a604f897423b5b12e6874328 /src/os/dir.go
parent5984ea71977d8436436a096902a32974b958c0bb (diff)
downloadgo-3d913a926675d8d6fcdc3cfaefd3136dfeba06e1.tar.xz
os: add ReadFile, WriteFile, CreateTemp (was TempFile), MkdirTemp (was TempDir) from io/ioutil
io/ioutil was a poorly defined collection of helpers. Proposal #40025 moved out the generic I/O helpers to io. This CL for proposal #42026 moves the OS-specific helpers to os, making the entire io/ioutil package deprecated. For #42026. Change-Id: I018bcb2115ef2ff1bc7ca36a9247eda429af21ad Reviewed-on: https://go-review.googlesource.com/c/go/+/266364 Trust: Russ Cox <rsc@golang.org> Trust: Emmanuel Odeke <emmanuel@orijtech.com> Run-TryBot: Russ Cox <rsc@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Emmanuel Odeke <emmanuel@orijtech.com>
Diffstat (limited to 'src/os/dir.go')
-rw-r--r--src/os/dir.go22
1 files changed, 21 insertions, 1 deletions
diff --git a/src/os/dir.go b/src/os/dir.go
index 1d90b970e7..5306bcb3ba 100644
--- a/src/os/dir.go
+++ b/src/os/dir.go
@@ -4,7 +4,10 @@
package os
-import "io/fs"
+import (
+ "io/fs"
+ "sort"
+)
type readdirMode int
@@ -103,3 +106,20 @@ func (f *File) ReadDir(n int) ([]DirEntry, error) {
// testingForceReadDirLstat forces ReadDir to call Lstat, for testing that code path.
// This can be difficult to provoke on some Unix systems otherwise.
var testingForceReadDirLstat bool
+
+// ReadDir reads the named directory,
+// returning all its directory entries sorted by filename.
+// If an error occurs reading the directory,
+// ReadDir returns the entries it was able to read before the error,
+// along with the error.
+func ReadDir(name string) ([]DirEntry, error) {
+ f, err := Open(name)
+ if err != nil {
+ return nil, err
+ }
+ defer f.Close()
+
+ dirs, err := f.ReadDir(-1)
+ sort.Slice(dirs, func(i, j int) bool { return dirs[i].Name() < dirs[j].Name() })
+ return dirs, err
+}