-
Notifications
You must be signed in to change notification settings - Fork 18k
bufio: make zero value of Reader useful #45374
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
How so? The program below doesn't hang.
|
Interesting, this program does hang: package main
import (
"bufio"
"io"
"strings"
)
func main() {
var br bufio.Reader
readers := []io.Reader{strings.NewReader("data\n")}
for _, r := range readers {
br.Reset(r)
br.ReadBytes('\n')
}
} |
It does not hang, when calling io.Copy. Because io.Copy calls src.WriteTo, bufio.Reader.WriteTo here, and in turn, bufio.Reader.WriteTo calls the internal reader's (strings.Reader here) WriteTo function. |
Isn't it OK just doing this: func (b *Reader) Reset(r io.Reader) {
// add this if statement
if len(b.buf) < minReadBufferSize {
b.buf = make([]byte, minReadBufferSize)
}
b.reset(b.buf, r)
} |
This will not save memory |
Yea, that's more or less what's proposed with saying "
I'm not sure what you're getting at. This issue isn't about saving memory, but making the zero value of |
Change https://golang.org/cl/307991 mentions this issue: |
Change https://golang.org/cl/345570 mentions this issue: |
Consider the following code pattern:
Currently, this pattern results in an infinite loop since the internal buffer of
bufio.Reader
is empty (resulting in no progress being made). Thebufio.Reader.Reset
method should initialize the internal buffer withdefaultBufSize
if it is empty.The text was updated successfully, but these errors were encountered: