DEV Community

Cover image for Attempting to Learn Go - Issuer 02

Attempting to Learn Go - Issuer 02

Steve Layton on June 15, 2019

GitHub Issuer Welcome back! Though if you haven’t read the first part, you may want to. We’re expanding on the code that we write last t...
Collapse
 
kunde21 profile image
Chad Kunde

Quick pointer, because it's common in learning Go.

  decoder := json.NewDecoder(ioutil.NopCloser(bytes.NewBuffer(p)))
  err = decoder.Decode(&Payload)

Is better done via Unmarshal:

  err = json.Unmarshal(p, &Payload)

Decoders should be used when the incoming data is an io.Reader, to handle processing streams of data without buffering everything first. Once the data is buffered into a byte slice (by ValidatePayload), then Unmarshal is much more efficient.

Collapse
 
shindakun profile image
Steve Layton

Good call! I used decoder in the first pass of the code and didn't think to swap over to unmarshal. I'll add a note to the post. 👍