bytes.Buffer.ReadAt?

1,505 views
Skip to first unread message

fri...@gmail.com

unread,
Mar 25, 2019, 2:06:30 AM3/25/19
to golang-nuts
Is there a reason I shouldn't submit a PR for bytes.Buffer.ReadAt?  It would at least help for some in memory tests.

--
Thanks,
fREW

roger peppe

unread,
Mar 25, 2019, 4:55:23 AM3/25/19
to fri...@gmail.com, golang-nuts
The reason bytes.Buffer doesn't implement io.ReaderAt is that it doesn't keep bytes in the buffer that have been read, so it's not possible to implement that method.

You can use bytes.Reader instead, which does implement io.ReaderAt and io.Seeker.
To make a bytes.Reader from a bytes.Buffer, you can do `bytes.NewReader(buf.Bytes())`.

  hope this helps,
    rog.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

fREW Schmidt

unread,
Mar 25, 2019, 9:57:39 AM3/25/19
to roger peppe, golang-nuts
Oh thanks, I'll switch my code to reader, though as far as I can tell,
bytes.Buffer doesn't discard already read data, it merely sets the
(private) offset. Maybe I just got luck though.

Thanks again!
--
fREW Schmidt
https://blog.afoolishmanifesto.com

Thomas Bushnell, BSG

unread,
Mar 25, 2019, 11:23:51 AM3/25/19
to fREW Schmidt, roger peppe, golang-nuts
Notice the logic in (*bytes.Buffer).grow which will throw away already read data, specifically in the case n <= c/2--m.

fREW Schmidt

unread,
Mar 25, 2019, 2:16:01 PM3/25/19
to Thomas Bushnell, BSG, roger peppe, golang-nuts
Ah my stuff just never got that big. Thank you!
Reply all
Reply to author
Forward
0 new messages