-
Notifications
You must be signed in to change notification settings - Fork 17.7k
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
proposal: io: add ReadSome #48182
Comments
Of course, that's just |
Why can't this be in a third-party library? |
This proposal has been added to the active column of the proposals project |
What about ReadPartial to emphasize the fact that you can handle partial results in this part of the code? |
It seems that a linter that checks whether you use the |
Based on the discussion above, this proposal seems like a likely decline. |
No change in consensus, so declined. |
The
io.Reader.Read
method is notoriously tricky, with lots of docs: https://pkg.go.dev/io#ReaderIn particular, it's common for people new to Go to write:
... and think they're reading N bytes. Especially when it almost always works. The
testing/iotest
package is easy to miss (and easier to just not use, even when you're aware of it).I regularly correct that pattern in code reviews when I see others make that mistake, and despite writing Go for over 11 years, I also made that mistake the other day, causing problems today.
The
io
package has these helpers that, in addition to doing as documented, also declare the author's intent:When the author writes the snippet at top, though, it's hard to know intent. Sometimes it's actually legit to read fewer:
It'd almost be nice to have a new "helper" in the
io
package like:Then all raw
io.Reader.Read
calls could be replaced withio.ReadFull
,io.ReadSome
etc, and a raw calls would then stand out as shady, warranting further inspection or annotation.People who wanted to be really strict could make their static analysis tool(s) of choice even forbid raw Read calls.
/cc @danderson @dsnet
The text was updated successfully, but these errors were encountered: