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.
io.Reader
ValidatePayload
Unmarshal
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. 👍
decoder
unmarshal
We're a place where coders share, stay up-to-date and grow their careers.
Quick pointer, because it's common in learning Go.
Is better done via Unmarshal:
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 (byValidatePayload), thenUnmarshalis much more efficient.Good call! I used
decoderin the first pass of the code and didn't think to swap over tounmarshal. I'll add a note to the post. 👍