Thanks! So using ioutil.ReadAll() followed by json.Unmarshal() can still be used, as this official example in the documentation shows
https://golang.org/pkg/net/http/#example_Get But is less efficient than using json.Decoder().Decode() itself How should I handle the EOF case? On Tuesday, March 20, 2018 at 11:26:18 AM UTC-7, Alex Efros 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.