-
Notifications
You must be signed in to change notification settings - Fork 18k
proposal: bytes: add Buffer.Peek #73794
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
Change https://go.dev/cl/674415 mentions this issue: |
Related Issues Related Code Changes (Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.) |
Or maybe we can just add something like this? func asReader(r io.Reader) reader {
if rr, ok := r.(reader); ok {
return rr
} else if bb, ok := r.(*bytes.Buffer); ok {
return bytesBufferAdapter{bb}
}
return bufio.NewReader(r)
}
type bytesBufferAdapter struct{ *bytes.Buffer }
func (a bytesBufferAdapter) Peek(n int) ([]byte, error) {
b := a.Bytes()
if len(b) < n {
return b, io.EOF
}
return b[:n], nil
} bufio already imports bytes anyway. Alternatively you could just do the same when passing the *bytes.Buffer... |
I think this functionality makes sense, but I was initially confused between this and the existing In retrospect I guess I might've named But since that ship has already sailed, perhaps this can be addressed with just some careful wording in the documentation, possibly including an analogy to Thinking down that path made me also wonder about a slightly different behavior:
This definition makes it more directly analogous to That would make it slightly different than the current Lines 59 to 69 in c8bf388
(the |
Proposal Details
Add a
Peek
method tobytes.Buffer
to avoid unnecessary wrapping when passing a buffer toimage.Decode
.See:
The text was updated successfully, but these errors were encountered: