a seekable byte buffer

334 views
Skip to first unread message

halmstad....@gmail.com

unread,
May 9, 2016, 12:47:32 AM5/9/16
to golang-nuts
Hi all,
I just found out that bytes.buffer doesn't support seeking, anyone have a good explanation why?
Anyways, I put together a basic seekable byte buffer here:

https://github.com/fncodr/godbase/blob/master/memfile.go

Peace

Jesse McNelis

unread,
May 9, 2016, 12:56:21 AM5/9/16
to halmstad....@gmail.com, golang-nuts
The reasons bytes.Buffer doesn't support seeking is that seeking
prevents the buffer from reusing space that has been read from.
Your buffer that supports seek will continue to grow with every call
to Write(), a bytes.Buffer will only grow if the amount of data being
written is more than what has previously been read from the buffer.

halmstad....@gmail.com

unread,
May 9, 2016, 1:14:54 AM5/9/16
to golang-nuts, halmstad....@gmail.com, jes...@jessta.id.au
Ah, too clever for me :) And unfortunately it prevents it from being used as a file replacement...

Uli Kunitz

unread,
May 9, 2016, 1:17:57 AM5/9/16
to golang-nuts, halmstad....@gmail.com, jes...@jessta.id.au
I agree a buffer is not a memory file. Note that bytes.Reader supports a Seek method. I suggest also to look at the Truncate method of bytes.Buffer, it may solve the actual issue one wants to address with a Seek on the buffer.


On Monday, May 9, 2016 at 6:56:21 AM UTC+2, Jesse McNelis wrote:

halmstad....@gmail.com

unread,
May 9, 2016, 1:43:50 AM5/9/16
to golang-nuts, halmstad....@gmail.com, jes...@jessta.id.au
For this particular adventure; I needed reading, writing and seeking in the same memory backed package. From what I see so far, there is nothing in the stdlib that fits.
Reply all
Reply to author
Forward
0 new messages