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
bufio: add function to read a certain amount of bytes #17064
Comments
Take a look at https://golang.org/pkg/io/#ReadFull. I think that is what you want. Although I'm not sure why you would use Peek and then Discard. |
Hm, I didn't see this function earlier, but it's closer to what I want, but not the perfect. |
Doesn't
do exactly this? |
@uluyol, that allocates, and the |
Look, I have this function in my project: // ReadSeveralBytes reads n bytes.
func (r *Reader) ReadSeveralBytes(n int) ([]byte, error) {
if n == 0 {
return nil, nil
}
peeked, err := r.buf.Peek(n)
if err != nil {
return nil, err
}
if _, err := r.buf.Discard(n); err != nil {
return nil, err
}
return peeked, nil
} If it will allocate a slice on every call of function, it will consume a lot of memory. But |
At least the naming of |
I agree. I'm going to close this issue. I'm happy we got |
@bradfitz So, when I want to read several bytes, what should I use? |
Just using
Users who need this already can do it, using the function you already wrote. I don't think it needs to be part of the standard library. I think it would do more harm (cognitive overload, documentation clutter) than good (benefiting a few users, reducing their need to write a small function). |
Ok, I agree with you.
And it will overwhelm library. Now I understood this. Thanks for explanation and sorry for wasting your time! |
In many programs, where I use
bufio
package, lacks the function, that can read certain amount of bytes.So now I should create function like this:
But it's inconvenient and I think, the way it works could be better.
I hope, what I'm not the only one, who wants it in
bufio
package.The text was updated successfully, but these errors were encountered: