[go-nuts] handling io.Reader of unknown length

797 views
Skip to first unread message

Nick Cronin

unread,
Apr 16, 2010, 8:30:26 PM4/16/10
to golang-nuts
I'm doing a simple http.Get request where the server does not provide
a Content-Length header and as such the Response.ContentLength is set
to -1. Unfortunately this is the first time I've encountered this
problem in a language that requires the buffer that is reading the
body of a Response to have a pre-defined length. Rather than do it
badly (such as just pre-defining a massive buffer) I was hoping for an
example of the best way to handle such a problem. My best guess is to
loop through the Reader using io.ReadFull with a buffer of say 1024
bytes until io.ErrUnexpectedEOF is hit.

Thanks


--
Subscription settings: http://groups.google.com/group/golang-nuts/subscribe?hl=en

Russ Cox

unread,
Apr 16, 2010, 8:41:58 PM4/16/10
to Nick Cronin, golang-nuts
If you don't mind reading it all into memory, use io.ReadAll.

Russ

Nick Cronin

unread,
Apr 16, 2010, 8:48:25 PM4/16/10
to r...@golang.org, golang-nuts
I did see a reference somewhere of io.ReadAll when I was looking but
couldn't find it in the docs. You mentioning it now made me think I
should look again. It's actually in io/ioutil - thank you
Reply all
Reply to author
Forward
0 new messages