bytes.Writer?

2,746 views
Skip to first unread message

Mitchell Hashimoto

unread,
Aug 17, 2013, 2:03:28 PM8/17/13
to golan...@googlegroups.com
Hello,

There is a bytes.Reader for turning a raw []byte into an io.Reader. Why is there no equivalent for io.Writer? I suppose I could use "bytes.Buffer" but I don't want it to grow. 

The reasoning is I want to copy an io.Reader where I know the length (so io.CopyN) into a []byte. Thoughts?

Best,
Mitchell

Ludwig Valda Vasquez

unread,
Aug 17, 2013, 2:15:14 PM8/17/13
to golan...@googlegroups.com
Why not just use Read method of io.Reader?

http://play.golang.org/p/8ebDizKbr2

суббота, 17 августа 2013 г., 22:03:28 UTC+4 пользователь Mitchell Hashimoto написал:

Ian Lance Taylor

unread,
Aug 17, 2013, 2:16:06 PM8/17/13
to Mitchell Hashimoto, golang-nuts
Pass your slice to bytes.NewBuffer. If you don't pass too many bytes,
it won't grow.

I don't think there are any length-restricted writers in the standard
library, as it's not a feature many people need. If there is a clear
use case for bytes.Writer or io.LimitWriter I suppose they could be
added, where by a clear use case I don't mean "I can write a program
that uses them" but "this important program or protocol would benefit
from them in this way."

Ian

Benjamin Measures

unread,
Aug 17, 2013, 6:49:33 PM8/17/13
to golan...@googlegroups.com
On Saturday, 17 August 2013 19:03:28 UTC+1, Mitchell Hashimoto wrote:
There is a bytes.Reader for turning a raw []byte into an io.Reader. Why is there no equivalent for io.Writer?

What would that mean? A writeTo on one side is the same as a readFrom on the other. 
 
The reasoning is I want to copy an io.Reader where I know the length (so io.CopyN) into a []byte. Thoughts?

 Just io.Read(p []byte) - it reads up to len(p) bytes into p: http://golang.org/pkg/io/#Reader

Mitchell Hashimoto

unread,
Aug 17, 2013, 6:54:35 PM8/17/13
to Benjamin Measures, golang-nuts
Benjamin,

On Sat, Aug 17, 2013 at 3:49 PM, Benjamin Measures
<saint....@gmail.com> wrote:
> On Saturday, 17 August 2013 19:03:28 UTC+1, Mitchell Hashimoto wrote:
>>
>> There is a bytes.Reader for turning a raw []byte into an io.Reader. Why is
>> there no equivalent for io.Writer?
>
>
> What would that mean? A writeTo on one side is the same as a readFrom on the
> other.

It means I can use a []byte as an io.Writer. Having a ReadFrom/WriteTo
implements other interfaces which simply don't satisfy this.

I realize it wouldn't be difficult by any means. It is an easy handful
of lines to write to implement it for []byte. My question was mostly
why doesn't the pair exist in the standard library or if there was a
better way to do what I'm doing.

>
>>
>> The reasoning is I want to copy an io.Reader where I know the length (so
>> io.CopyN) into a []byte. Thoughts?
>
>
> Just io.Read(p []byte) - it reads up to len(p) bytes into p:
> http://golang.org/pkg/io/#Reader

It reads UP TO len(p). If it reads a part and it would start blocking
it might return what it has "so far". I would still have to write a
helper function to copy it all. What I want is a "read until EOF"
(which is io.Copy), so I want an io.Writer implementation so I can use
it on the left side of io.Copy.

For now I've just used "bytes.NewBuffer" since I know exactly how many
bytes will go into there.

Best,
Mitchell

>
> --
> You received this message because you are subscribed to a topic in the
> Google Groups "golang-nuts" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/golang-nuts/FB4OtiRm28E/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> golang-nuts...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.

Benjamin Measures

unread,
Aug 17, 2013, 8:22:37 PM8/17/13
to golan...@googlegroups.com, Benjamin Measures
On Saturday, 17 August 2013 23:54:35 UTC+1, Mitchell Hashimoto wrote:
On Sat, Aug 17, 2013 at 3:49 PM, Benjamin Measures
<saint....@gmail.com> wrote:
> On Saturday, 17 August 2013 19:03:28 UTC+1, Mitchell Hashimoto wrote:
>>
>> There is a bytes.Reader for turning a raw []byte into an io.Reader. Why is
>> there no equivalent for io.Writer?
>
> What would that mean? A writeTo on one side is the same as a readFrom on the
> other.

It means I can use a []byte as an io.Writer. Having a ReadFrom/WriteTo
implements other interfaces which simply don't satisfy this.

I don't think I meant the writerTo interface literally. I was trying to get at the point that if you can't push, pull.
 
>> The reasoning is I want to copy an io.Reader where I know the length (so
>> io.CopyN) into a []byte. Thoughts?
>
>  Just io.Read(p []byte) - it reads up to len(p) bytes into p:
> http://golang.org/pkg/io/#Reader

It reads UP TO len(p). If it reads a part and it would start blocking
it might return what it has "so far". I would still have to write a
helper function to copy it all.

Tbh, I had neglected that aspect of an io.Reader. However, that sounds like a useful helper function, helpful enough to be included as an io.ReadAtLeast, or even better, io.ReadFull: http://golang.org/pkg/io/#ReadFull

Kevin Gillette

unread,
Aug 18, 2013, 1:14:04 AM8/18/13
to golan...@googlegroups.com
On Saturday, August 17, 2013 4:49:33 PM UTC-6, Benjamin Measures wrote:
 Just io.Read(p []byte) - it reads up to len(p) bytes into p: http://golang.org/pkg/io/#Reader

You probably mean io.ReadFull, as that reads precisely len(buf) bytes or returns a non-nil error if it can't. io.Readers are allowed to (and by convention often do) read less than the space available in the provided buffer. 

Mitchell Hashimoto

unread,
Aug 18, 2013, 11:22:59 AM8/18/13
to Ludwig Valda Vasquez, golang-nuts
On Sat, Aug 17, 2013 at 11:15 AM, Ludwig Valda Vasquez <bre...@gmail.com> wrote:
> Why not just use Read method of io.Reader?
>
> http://play.golang.org/p/8ebDizKbr2

Read the rest of the thread and you'll see your answer has already
been answered.

>
> суббота, 17 августа 2013 г., 22:03:28 UTC+4 пользователь Mitchell Hashimoto
> написал:
>>
>> Hello,
>>
>> There is a bytes.Reader for turning a raw []byte into an io.Reader. Why is
>> there no equivalent for io.Writer? I suppose I could use "bytes.Buffer" but
>> I don't want it to grow.
>>
>> The reasoning is I want to copy an io.Reader where I know the length (so
>> io.CopyN) into a []byte. Thoughts?
>>
>> Best,
>> Mitchell
>
Reply all
Reply to author
Forward
0 new messages