-
Notifications
You must be signed in to change notification settings - Fork 18k
proposal: io: add interface type StringBytesWriter #25464
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
Comments
An alternative name: io.TextWriter |
I'm not sure I follow how exposing this interface would improve efficiency. Interfaces are satisfied implicitly, so it does not matter whether or not the I'm also not sure that exposing interfaces that combine existing exposed interfaces is that helpful. Perhaps a better proposed change would be to expose |
@mvdan you'd get better efficiency when you provide external packages the interface I described for writing instead of just an io.Writer when the writer is intended for string data. The point is that, if you've already got a string and you need to write it to an io.Writer, you need to convert it to a []byte, and this is an allocation. And if you've only got an io.Writer for writing, and you want to write a single byte, you need to make a slice out of it to pass it into Write. (But, as part of the Go language itself, strings and single bytes can be copied directly to any []byte.) |
@dchenk
Can you please list a couple from popular repos? It might add weight to the proposal to see such examples in the wild. |
@dchenk I understand the point of writing single bytes and strings. My point is that you can do this optimization without the
But note that not even that is necessary. You could do:
This is what many parts of the standard library do. This way,
|
@meirf
and
|
If you use
and
|
To be a viable proposal, this issue needs more detail. Please include concrete examples (ideally drawn from experience reports) that illustrate the motivating problem, then show how those examples would be improved by it. (For example, if the motivating problem is a performance issue, quantify the impact on a real program: how much time is being spent today in type-assertions that this proposal would eliminate?) |
I propose that we add to package io:
This would solve a lot of inconveniences and improve efficiency in many places where a Writer represents something that writes string (or textual) data.
I often find that it’s undesirable for some exported functions to take as arguments or return a type like *bytes.Buffer or *bufio.Writer or *strings.Builder when users of a package in these cases should only be writing data, not otherwise modifying the state of the concrete type. All of these three types (*bytes.Buffer, *bufio.Writer, and *strings.Builder) and many more outside the stdlib already implement the proposed StringBytesWriter interface: it can be much more efficient to write a single byte or directly copy a string to the Writer without having to allocate a slice.
The text was updated successfully, but these errors were encountered: