Hi!

On Tue, Mar 20, 2018 at 10:37:40AM -0700, st ov wrote:
> json.Unmarshal(resp.Body, &data)

This one is invalid.

> json.NewDecoder(resp.Body).Decode(&data)
> 
> or
> 
> b, _ := ioutil.ReadAll(resp.Body)
> json.Unmarshal(b, &data)

In the ReadAll case you'll have to allocate []byte in memory to store
whole response, which is expected to be less effective than NewDecoder
case which may use less memory to process whole response.

Another difference is NewDecoder() may stop reading before EOF in case it
notice syntax error or if response contain more than one JSON element.

In short, use NewDecoder when you've io.Reader, and Unmarshal when you've
[]byte.

-- 
                        WBR, Alex.

-- 
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 [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to