-
Notifications
You must be signed in to change notification settings - Fork 17.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
os: add ReadDir, ReadFile, WriteFile, CreateTemp, MkdirTemp & deprecate io/ioutil #42026
Comments
Would it make sense to put I would argue that they fit naturally with the new |
( |
Actually, even |
@bcmills, io/fs already has ReadDir and ReadFile. It would be fine to add CreateTemp and MkdirTemp if/when io/fs adds any writing operations. But it probably wouldn't make sense to put them only in io/fs. Reading from the operating system's file systems is insanely common. For example, even if we were starting over we would certainly keep os.Open(name) rather than tell people to use fs.Open(os.Dir("/"), name) or os.Dir("/").Open(name) (neither of which would be correct on Windows anyway). |
Accepting this issue will obsolete #19660, which proposes renaming ioutil. |
Change https://golang.org/cl/266365 mentions this issue: |
Change https://golang.org/cl/266364 mentions this issue: |
Change https://golang.org/cl/266366 mentions this issue: |
I've posted the CLs that would implement this change in case anyone wants to see what the diffs look like. |
Based on the discussion, this seems like a likely accept. |
Getting rid of ioutil is a good conceptual clean up. I know when I first learned Go, I was confused about what was in io vs. ioutil vs. bufio. io itself is a little tricky to understand, but with the cleanup, the explanation is pretty clear. I can imagine writing a blog post for beginners along the lines of:
The only thing that seems odd given that description is that FileInfo and FileMode are in io/fs instead of io itself. On the one hand, they're part of a "virtual file" so you'd think they'd be in io. OTOH, they only make sense as part of a "virtual filesystem" in which case they should be in io/fs… |
Instead of "virtual files" say "virtual data streams" and you're all set! |
No change in consensus, so accepted. |
…s 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>
…Temp As part of #42026, these helpers from io/ioutil were moved to os. (ioutil.TempFile and TempDir became os.CreateTemp and MkdirTemp.) Update the Go tree to use the preferred names. As usual, code compiled with the Go 1.4 bootstrap toolchain and code vendored from other sources is excluded. ReadDir changes are in a separate CL, because they are not a simple search and replace. For #42026. Change-Id: If318df0216d57e95ea0c4093b89f65e5b0ababb3 Reviewed-on: https://go-review.googlesource.com/c/go/+/266365 Trust: Russ Cox <rsc@golang.org> Run-TryBot: Russ Cox <rsc@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
All the code in ioutil just forwards functionality to code in either the io or os packages, per issue 42026. This change adds the "Deprecated" marker to all the functions in this package. For #42026 Fixes #51927 Change-Id: Ia807bc5c0edb06cc80ec7e35917dcfe2ad50f0ea GitHub-Last-Rev: 3c3603f GitHub-Pull-Request: #51961 Reviewed-on: https://go-review.googlesource.com/c/go/+/395918 Reviewed-by: Emmanuel Odeke <emmanuel@orijtech.com> Trust: Emmanuel Odeke <emmanuel@orijtech.com> Run-TryBot: Emmanuel Odeke <emmanuel@orijtech.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
Change https://go.dev/cl/399854 mentions this issue: |
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>
使用os包中的ReadFile、ReadDir,详情见golang/go#42026
使用os包中的ReadFile、ReadDir,详情见golang/go#42026
As of go1.16, io/util package has been deprecated See issue, golang/go#42026 Signed-off-by: Rui Chen <rui@chenrui.dev>
As of go1.16, io/util package has been deprecated See issue, golang/go#42026 Signed-off-by: Rui Chen <rui@chenrui.dev> Signed-off-by: Rui Chen <rui@chenrui.dev>
Implement go-staticcheck suggestions. Replaces ioutil with new functions in the os package. Each function in the os package is a direct replacement for the previously used functions in the ioutil package. For more context see these: - go-critic/go-critic#1019 - golang/go#42026
`io/ioutil` has been deprecated since go-1.16 [1]. We should use `io` and `os` instead. [1] golang/go#42026
Implement go-staticcheck suggestions. Replaces ioutil with new functions in the os package. Each function in the os package is a direct replacement for the previously used functions in the ioutil package. For more context see these: - go-critic/go-critic#1019 - golang/go#42026
As of go1.16, io/util package has been deprecated See issue, golang/go#42026 Signed-off-by: Rui Chen <rui@chenrui.dev> Signed-off-by: Rui Chen <rui@chenrui.dev>
According to - golang/go#40025 - golang/go#42026 `ioutil` is deprecated, `io` and `os` packages should be used instead.
According to - golang/go#40025 - golang/go#42026 `ioutil` is deprecated, `io` and `os` packages should be used instead.
io/ioutil, like most things with util in the name, has turned out to be a poorly defined and hard to understand collection of things.
As part of #40025, we migrated Discard, NopCloser, and ReadAll to package io.
(They remain in io/ioutil for compatibility, of course, but new code should prefer the ones in io.)
What's left is a few OS file system helpers: ReadDir, ReadFile, TempDir, TempFile, and WriteFile.
I propose we migrate all of these to package os, with a few adjustments.
(Again, they would remain here for compatibility, but new code would prefer the ones in os.)
At that point, io/ioutil would become entirely deprecated.
The signatures would be:
Notes:
os.ReadDir returns []DirEntry, in contrast to ioutil.ReadDir's []FileInfo.
(Providing a helper that returns []DirEntry is one of the primary motivations for this change.)
os.ReadFile is identical to ioutil.ReadFile.
os.WriteFile is identical to ioutil.WriteFile.
I looked into whether the permission bits should be dropped, but Go code in the wild uses a variety of popular settings (for example: 0666, 0644, 0600, 0700, 0777).
os.MkdirTemp is identical to ioutil.TempDir.
It cannot be named os.TempDir because that already exists and returns the default temporary directory.
MkdirTemp seems like it makes clear that the directory is being created and will appear next to Mkdir in docs.
I looked into whether the directory argument should be dropped, but about 20% of calls in the wild don't use the empty string.
os.CreateTemp is identical to ioutil.TempFile.
It could be named os.TempFile, but calling it os.CreateTemp is consistent with os.MkdirTemp, makes clear that the file is being created (using TempFile for this would be a function surprisingly different from TempDir), and will appear next to Create in docs.
I looked into whether the directory argument should be dropped, but about 25% of calls in the wild don't use the empty string.
The text was updated successfully, but these errors were encountered: