bytes.Buffer Seek()

3,232 views
Skip to first unread message

fango

unread,
Sep 6, 2010, 10:13:34 AM9/6/10
to golang-nuts
For the dbm code I'm working on, I would like to use
io.ReadWriteSeeker so not only os.File, but also (encryption, net)
wrappers can serve the persistent hash. Even the in mem bytes.Buffer,
if it provides Seek() method, but unfortunately not yet there.
Question is: Any plan to add it in? Or, allow me to contribute it?

A demo code is:

package main

import (
"io"
"os"
"bytes"
)

type H struct {
rw io.ReadWriteSeeker
}

func New(rw io.ReadWriteSeeker) *H {
return &H{rw: rw}
}

func main() {
f, _ := os.Open("rws.go", os.O_RDWR, 0666)
defer f.Close()
_ = New(f)

b := new(bytes.Buffer)
_ = New(b) // Error, missing Seek method
}

Cheers,
Fango

Evan Shaw

unread,
Sep 6, 2010, 10:23:32 AM9/6/10
to fango, golang-nuts
On Mon, Sep 6, 2010 at 9:13 AM, fango <fan.h...@gmail.com> wrote:
> For the dbm code I'm working on, I would like to use
> io.ReadWriteSeeker so not only os.File, but also (encryption, net)
> wrappers  can serve the persistent hash. Even the in mem bytes.Buffer,
> if it provides Seek() method, but unfortunately not yet there.
> Question is: Any plan to add it in? Or, allow me to contribute it?

There have been a couple CLs on the subject:
http://groups.google.com/group/golang-dev/browse_thread/thread/33e4e52fd2ecaa2c/427c4612473378b9
http://groups.google.com/group/golang-dev/browse_thread/thread/70b8f932f5456d16/6b59098e2d1fe39f

It seems like there's a possibility of adding it (or adding another
structure that allows it). Someone just needs to be willing to see it
through.

- Evan

Russ Cox

unread,
Sep 6, 2010, 7:22:52 PM9/6/10
to fango, golang-nuts
On Mon, Sep 6, 2010 at 10:13, fango <fan.h...@gmail.com> wrote:
> For the dbm code I'm working on, I would like to use
> io.ReadWriteSeeker so not only os.File, but also (encryption, net)
> wrappers  can serve the persistent hash. Even the in mem bytes.Buffer,
> if it provides Seek() method, but unfortunately not yet there.
> Question is: Any plan to add it in? Or, allow me to contribute it?

No. bytes.Buffer has two separate offsets, one for reading
and one for writing. Which one should Seek manipulate?
Worse, bytes.Buffer's interface is written so that it can be
a buffer: it only needs to store data that has been written
but not yet read. If you alternate between writing to and
reading from the buffer, it will reuse the same memory instead
of growing without bound. If you add Seek, it can no longer
do that.

Russ

fango

unread,
Sep 6, 2010, 7:55:52 PM9/6/10
to golang-nuts

> No.  bytes.Buffer has two separate offsets...
Thanks. Point taken. But I still need a in mem readwriteseeker. Has
[1] had any result out? If no, I'll forge ahead. I guess bytes.RWS
(call it Rewiser) would be a better name than bytes.File 'cause the
FILE is already heavily overloaded and can means anything from the one
in dustbin to the top secrecies. Kidding aside, I'll follow closely to
byte.Buffer, with one offset, growable, and does only three ops as the
name specified.

Cheers,
Fango

[1] http://codereview.appspot.com/179074. Thanks Evan, I did the
search but stopped at the first hit thought it was a test

Rob 'Commander' Pike

unread,
Sep 6, 2010, 8:57:54 PM9/6/10
to fango, golang-nuts
I suspect all you need is the ability to overwrite the unread data,
which you can do by overwriting the result of Bytes(). Adding seek to
this model or providing an alternate implementation just to provide
overwrite capability will complicate matters tremendously.

I am very nervous about trying to fit a Seekable item into the simple
primitives of bytes and bytes.Buffer.

-rob

fango

unread,
Sep 6, 2010, 10:17:15 PM9/6/10
to golang-nuts
Before seeing this, I've done http://codereview.appspot.com/2106046
Please help review. All I need is a growable byte array that satisfy
io.ReadWriteSeeker, and the implementation is trivial.

Cheers,
Fango

fango

unread,
Sep 6, 2010, 10:31:54 PM9/6/10
to golang-nuts
On 2nd thought. You're right. It is just a slice. offset is actually
len(). I've abandoned the CL.

cheers,
Fango

On Sep 7, 10:17 am, fango <fan.how...@gmail.com> wrote:
> Before seeing this, I've donehttp://codereview.appspot.com/2106046
Reply all
Reply to author
Forward
0 new messages