ioutil.ReadAll considered harmful
Closed this issue · 6 comments
http://jmoiron.net/blog/crossing-streams-a-love-letter-to-ioreader/ has some context on reasons to avoid a ReadAll. This fact is reflected in the current source - from the source:
// TODO: could decode while reading instead
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
var response response
json.Unmarshal(body, &response)
If I'm undersatnding correctly, I believe this TODO could be done by essentially doing
var response response
err = json.NewDecoder(resp.Body).Decode(&response)
if err != nil {
return "", err
}
and it would be beneficial for performance
Thanks for the article. I'll take a look before making the switch.
I've seen some reasons to read the entire body for HTTP/1.1 but I don't think they apply for HTTP/2.0. Decode and ReadAll behave a bit differently, but I imagine Decode would work just as well in this case.
Nathan.
Pretty convincing article. :D
yeah I found it compelling, glad you did too, it certainly caused me to change some of my code, haha
Awesome! :D
I like that it removes a dependency to, no need to bring in ioutil.
Cheers.