Description
I find myself constantly needing to look up the documentation for io
and io/ioutil
to figure out which package contains the thing I want to use.
The root of the problem is that ioutil
isn't a coherent abstraction boundary. The suffix util
doesn't add any information — if a package has no utility, it should not exist! — and the remaining information in the package name doesn't distinguish it from io
. (See also the "Bad package names" section of https://blog.golang.org/package-names.)
From what I can tell, the main purpose of separating out ioutil
is to reduce the dependencies of io
: specifically, the dependencies on bytes
, os
, and sort
.
In a potential Go 2, I propose to resolve those dependencies by refactoring the ioutil
package as follows:
-
Move
Discard
,NopCloser
, andReadAll
into packageio
:NopCloser
has no dependencies.Discard
depends onsync
, whichio
already depends on.ReadAll
uses abytes.Buffer
internally, but doesn't actually need very much of its API; it can be rewritten easily to avoid that dependency.
-
Move
ReadDir
,ReadFile
,TempDir
,TempFile
, andWriteFile
to a new package,io/fileio
.- Rename them to omit the (now-redundant)
File
suffix:fileio.Read
instead ofioutil.ReadFile
.
- Rename them to omit the (now-redundant)
Activity
ghost commentedon Mar 23, 2017
io.ReadFull / ioutil.ReadAll pair is especially frustrating.
pwaller commentedon Mar 23, 2017
To take the logic a bit further with respect to removing chattiness, how about (or: why not?) one step further:
io/file
instead ofio/fileio
. Then it'sfile.Read
instead offileio.Read
.file
andio
together seem tautological. Especially when you throw in Read/Write, etc.Sounding them out:
file.Read
,file.Write
,file.Temp
,file.ReadDir
,file.TempDir
, etc. Side thought: Maybe the latter two would read better asdir.Read
anddir.Temp
, if there was aio/dir
package?There may be good arguments for
io/fileio
instead ofio/file
? One that comes to mind is that people might want to call variablesdir
andfile
. Not sure if that's a much of a problem in practice or not.[-]proposal: io/ioutil (Go 2): rename to io/fileio or similar[/-][+]proposal: io/ioutil: rename to io/fileio or similar[/+]cristaloleg commentedon Jul 9, 2020
Looks like this will be a huge problem, same as
net/url
orpath
.cespare commentedon Jul 9, 2020
I agree with @cristaloleg: this is absolutely a problem in practice.
file
anddir
are incredibly common, useful variable names. To quote the Package names blog post from 5 years ago:changkun commentedon Dec 17, 2020
Should this proposal be closed because of #42026 ?
ianlancetaylor commentedon Dec 18, 2020
Yes, thanks.