Something to consider is that NewDecoder supports JSON streams, e.g. {"a":
"b"}{"c": "d"}{"e": "f" }

Each call to Decode will decode one object from the stream, but the
underlying reader is not necessarily guaranteed to be at EOF. See:
https://ahmet.im/blog/golang-json-decoder-pitfalls/

I don't have the same conclusion as the article, and think that using the
stream syntax is often useful, but care must be taken to ensure that the
stream is fully consumed and closed.

Jonathan

On Tue, Mar 20, 2018 at 11:25 AM, Alex Efros <power...@powerman.name> wrote:

> 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 golang-nuts+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>



-- 
Jonathan Yu / *@jawnsy* on LinkedIn <https://linkedin.com/in/jawnsy>,
Twitter <https://twitter.com/jawnsy>, GitHub <https://github.com/jawnsy>,
Facebook <https://facebook.com/jawnsy>
*“Ever tried. Ever failed. No matter. Try again. Fail again. Fail better.”* —
Samuel Beckett, Worstward Ho (1983)

“In an adaptive environment, winning comes from adapting to change by
continuously experimenting and identifying new options more quickly and
economically than others. The classical strategist's mantra of sustainable
competitive advantage becomes one of serial temporary advantage.” — Navigating
the Dozens of Different Strategy Options
<https://hbr.org/2015/06/navigating-the-dozens-of-different-strategy-options>
 (HBR)

-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to