copying / cloning an io.Reader

4,159 views
Skip to first unread message

Johann Höchtl

unread,
Jan 17, 2014, 1:20:48 PM1/17/14
to golan...@googlegroups.com
A function returns an  io.Reader. When I pass that reader to eg. json.UnMarshal, the reader get's consumed. How can I clone that reader so to re-use it?

Brad Fitzpatrick

unread,
Jan 17, 2014, 1:24:24 PM1/17/14
to Johann Höchtl, golang-nuts
Assume an io.Reader is hooked up to a satellite dish receiving a message (maybe a 1TB message) from an alien civilization who only transmits their message once every thousand years.

There's no "rewinding" that.

If you want a copy, you need to copy it as you read it, perhaps to disk or memory, depending on its size.

io.TeeReader + bytes.Buffer is a common approach.




On Fri, Jan 17, 2014 at 10:20 AM, Johann Höchtl <johann....@gmail.com> wrote:
A function returns an  io.Reader. When I pass that reader to eg. json.UnMarshal, the reader get's consumed. How can I clone that reader so to re-use it?

--
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/groups/opt_out.

Johann Höchtl

unread,
Jan 17, 2014, 3:51:29 PM1/17/14
to Brad Fitzpatrick, golang-nuts
On 2014-01-17 19:24, Brad Fitzpatrick wrote:
> Assume an io.Reader is hooked up to a satellite dish receiving a message
> (maybe a 1TB message) from an alien civilization who only transmits
> their message once every thousand years.
>
> There's no "rewinding" that.
>
> If you want a copy, you need to copy it as you read it, perhaps to disk
> or memory, depending on its size.
>
> io.TeeReader + bytes.Buffer is a common approach.
>
>
Yes, seems to be black-out day, this works of course

buf, _ := ioutil.ReadAll(mdjsonreader)
minimaljsonbuffer := bytes.NewBuffer(buf)
mdjson := bytes.NewBuffer(buf)
>

Kevin Gillette

unread,
Jan 17, 2014, 4:20:45 PM1/17/14
to golan...@googlegroups.com
If the function is completely deterministic, then it is usually better to just call the function again to get a fresh io.Reader that'll produce identical content, rather than buffering it.

jeremy....@gmail.com

unread,
Jan 17, 2014, 8:43:56 PM1/17/14
to golan...@googlegroups.com
Why not use Multiwriter and Copy ?
Reply all
Reply to author
Forward
0 new messages