[go] io/ioutil: provide an equivalent for the deprecated ReadDir

432 views
Skip to first unread message

Daniel Martí (Gerrit)

unread,
Apr 12, 2022, 2:18:15 AM4/12/22
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Daniel Martí has uploaded this change for review.

View Change

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
---
M src/io/ioutil/ioutil.go
1 file changed, 39 insertions(+), 0 deletions(-)

diff --git a/src/io/ioutil/ioutil.go b/src/io/ioutil/ioutil.go
index 9921c2a..7bcd4f2 100644
--- a/src/io/ioutil/ioutil.go
+++ b/src/io/ioutil/ioutil.go
@@ -55,6 +55,17 @@
// 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(name)
+// if err != nil { ... }
+// infos := make([]fs.FileInfo, len(entries))
+// for i, entry := range entries {
+// info, err := entry.Info()
+// if err != nil { ... }
+// infos[i] = info
+// }
func ReadDir(dirname string) ([]fs.FileInfo, error) {
f, err := os.Open(dirname)
if err != nil {

To view, visit change 399854. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I76d46cd7d68fc609c873821755fdcfc299ffd56c
Gerrit-Change-Number: 399854
Gerrit-PatchSet: 1
Gerrit-Owner: Daniel Martí <mv...@mvdan.cc>
Gerrit-Reviewer: Daniel Martí <mv...@mvdan.cc>
Gerrit-MessageType: newchange

Daniel Martí (Gerrit)

unread,
Apr 12, 2022, 2:19:05 AM4/12/22
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Daniel Martí uploaded patch set #2 to this change.

View Change

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
---
M src/io/ioutil/ioutil.go
1 file changed, 39 insertions(+), 0 deletions(-)

To view, visit change 399854. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I76d46cd7d68fc609c873821755fdcfc299ffd56c
Gerrit-Change-Number: 399854
Gerrit-PatchSet: 2
Gerrit-Owner: Daniel Martí <mv...@mvdan.cc>
Gerrit-Reviewer: Daniel Martí <mv...@mvdan.cc>
Gerrit-CC: Gopher Robot <go...@golang.org>
Gerrit-MessageType: newpatchset

Bryan Mills (Gerrit)

unread,
Apr 12, 2022, 9:38:45 AM4/12/22
to Daniel Martí, goph...@pubsubhelper.golang.org, Bryan Mills, Gopher Robot, golang-co...@googlegroups.com

Attention is currently required from: Daniel Martí.

Patch set 2:Code-Review +1

View Change

1 comment:

  • File src/io/ioutil/ioutil.go:

    • Patch Set #2, Line 63: // infos := make([]fs.FileInfo, len(entries))

      (nit) I'd prefer to allocate this slice with length 0 and use `append`, rather than risk returning it with trailing nils in case of an error.

To view, visit change 399854. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I76d46cd7d68fc609c873821755fdcfc299ffd56c
Gerrit-Change-Number: 399854
Gerrit-PatchSet: 2
Gerrit-Owner: Daniel Martí <mv...@mvdan.cc>
Gerrit-Reviewer: Bryan Mills <bcm...@google.com>
Gerrit-Reviewer: Daniel Martí <mv...@mvdan.cc>
Gerrit-Reviewer: Gopher Robot <go...@golang.org>
Gerrit-Attention: Daniel Martí <mv...@mvdan.cc>
Gerrit-Comment-Date: Tue, 12 Apr 2022 13:38:41 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: Yes
Gerrit-MessageType: comment

Daniel Martí (Gerrit)

unread,
Apr 19, 2022, 4:08:25 AM4/19/22
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Attention is currently required from: Daniel Martí.

Daniel Martí uploaded patch set #3 to this change.

View Change

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
---
M src/io/ioutil/ioutil.go
1 file changed, 39 insertions(+), 0 deletions(-)

To view, visit change 399854. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I76d46cd7d68fc609c873821755fdcfc299ffd56c
Gerrit-Change-Number: 399854
Gerrit-PatchSet: 3
Gerrit-Owner: Daniel Martí <mv...@mvdan.cc>
Gerrit-Reviewer: Bryan Mills <bcm...@google.com>
Gerrit-Reviewer: Daniel Martí <mv...@mvdan.cc>
Gerrit-Reviewer: Gopher Robot <go...@golang.org>
Gerrit-Attention: Daniel Martí <mv...@mvdan.cc>
Gerrit-MessageType: newpatchset

Daniel Martí (Gerrit)

unread,
Apr 19, 2022, 4:08:36 AM4/19/22
to goph...@pubsubhelper.golang.org, Bryan Mills, Gopher Robot, golang-co...@googlegroups.com

Attention is currently required from: Bryan Mills.

View Change

1 comment:

  • File src/io/ioutil/ioutil.go:

    • (nit) I'd prefer to allocate this slice with length 0 and use `append`, rather than risk returning i […]

      Good point; done. I tend to prefer avoiding append for the sake of performance, as it's technically not needed, but if we may return early I agree that avoiding append adds a sharp edge that we could do without.

To view, visit change 399854. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I76d46cd7d68fc609c873821755fdcfc299ffd56c
Gerrit-Change-Number: 399854
Gerrit-PatchSet: 3
Gerrit-Owner: Daniel Martí <mv...@mvdan.cc>
Gerrit-Reviewer: Bryan Mills <bcm...@google.com>
Gerrit-Reviewer: Daniel Martí <mv...@mvdan.cc>
Gerrit-Reviewer: Gopher Robot <go...@golang.org>
Gerrit-Attention: Bryan Mills <bcm...@google.com>
Gerrit-Comment-Date: Tue, 19 Apr 2022 08:08:30 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
Comment-In-Reply-To: Bryan Mills <bcm...@google.com>
Gerrit-MessageType: comment

Bryan Mills (Gerrit)

unread,
Apr 19, 2022, 11:46:24 AM4/19/22
to Daniel Martí, goph...@pubsubhelper.golang.org, Bryan Mills, Gopher Robot, golang-co...@googlegroups.com

Attention is currently required from: Daniel Martí.

Patch set 3:Run-TryBot +1Code-Review +1

View Change

    To view, visit change 399854. To unsubscribe, or for help writing mail filters, visit settings.

    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: I76d46cd7d68fc609c873821755fdcfc299ffd56c
    Gerrit-Change-Number: 399854
    Gerrit-PatchSet: 3
    Gerrit-Owner: Daniel Martí <mv...@mvdan.cc>
    Gerrit-Reviewer: Bryan Mills <bcm...@google.com>
    Gerrit-Reviewer: Daniel Martí <mv...@mvdan.cc>
    Gerrit-Reviewer: Gopher Robot <go...@golang.org>
    Gerrit-Attention: Daniel Martí <mv...@mvdan.cc>
    Gerrit-Comment-Date: Tue, 19 Apr 2022 15:46:19 +0000
    Gerrit-HasComments: No
    Gerrit-Has-Labels: Yes
    Gerrit-MessageType: comment

    Ian Lance Taylor (Gerrit)

    unread,
    Apr 19, 2022, 12:18:59 PM4/19/22
    to Daniel Martí, goph...@pubsubhelper.golang.org, Gopher Robot, Bryan Mills, golang-co...@googlegroups.com

    Attention is currently required from: Daniel Martí.

    Patch set 3:Run-TryBot +1Auto-Submit +1Code-Review +2

    View Change

      To view, visit change 399854. To unsubscribe, or for help writing mail filters, visit settings.

      Gerrit-Project: go
      Gerrit-Branch: master
      Gerrit-Change-Id: I76d46cd7d68fc609c873821755fdcfc299ffd56c
      Gerrit-Change-Number: 399854
      Gerrit-PatchSet: 3
      Gerrit-Owner: Daniel Martí <mv...@mvdan.cc>
      Gerrit-Reviewer: Bryan Mills <bcm...@google.com>
      Gerrit-Reviewer: Daniel Martí <mv...@mvdan.cc>
      Gerrit-Reviewer: Gopher Robot <go...@golang.org>
      Gerrit-Reviewer: Ian Lance Taylor <ia...@google.com>
      Gerrit-Attention: Daniel Martí <mv...@mvdan.cc>
      Gerrit-Comment-Date: Tue, 19 Apr 2022 16:18:55 +0000

      Gopher Robot (Gerrit)

      unread,
      Apr 19, 2022, 12:19:30 PM4/19/22
      to Daniel Martí, goph...@pubsubhelper.golang.org, golang-...@googlegroups.com, Bryan Mills, golang-co...@googlegroups.com

      Gopher Robot submitted this change.

      View Change


      Approvals: Bryan Mills: Looks good to me, but someone else must approve; Run TryBots Ian Lance Taylor: Looks good to me, approved; Run TryBots; Automatically submit change Gopher Robot: TryBots succeeded
      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 <bcm...@google.com>
      Run-TryBot: Bryan Mills <bcm...@google.com>
      TryBot-Result: Gopher Robot <go...@golang.org>
      Reviewed-by: Ian Lance Taylor <ia...@google.com>
      Run-TryBot: Ian Lance Taylor <ia...@google.com>
      Auto-Submit: Ian Lance Taylor <ia...@google.com>
      ---
      M src/io/ioutil/ioutil.go
      1 file changed, 46 insertions(+), 0 deletions(-)

      diff --git a/src/io/ioutil/ioutil.go b/src/io/ioutil/ioutil.go
      index 9921c2a..6a1d691 100644

      --- a/src/io/ioutil/ioutil.go
      +++ b/src/io/ioutil/ioutil.go
      @@ -55,6 +55,17 @@
      // 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 {

      To view, visit change 399854. To unsubscribe, or for help writing mail filters, visit settings.

      Gerrit-Project: go
      Gerrit-Branch: master
      Gerrit-Change-Id: I76d46cd7d68fc609c873821755fdcfc299ffd56c
      Gerrit-Change-Number: 399854
      Gerrit-PatchSet: 4
      Gerrit-Owner: Daniel Martí <mv...@mvdan.cc>
      Gerrit-Reviewer: Bryan Mills <bcm...@google.com>
      Gerrit-Reviewer: Daniel Martí <mv...@mvdan.cc>
      Gerrit-Reviewer: Gopher Robot <go...@golang.org>
      Gerrit-Reviewer: Ian Lance Taylor <ia...@google.com>
      Gerrit-MessageType: merged
      Reply all
      Reply to author
      Forward
      0 new messages