Encode aren't exact mirrors of each other.
Encode writes a trailing newline, but decode doesn't read this newline:
https://github.com/golang/go/blob/f78a4c84ac8ed44aaf331989aa32e40081fd8f13/src/encoding/json/stream.go#L193
I became aware of this via a nasty docker bug:
https://github.com/docker/docker/issues/14203
I can't find any other discussion about the problem on this list, hopefully
others can chime in with references to why this decision was made.
As far as I can see, your solution to use ioutil.Discard is correct and
reasonable.
A strict reading of the documentation supports the current behaviour:
https://golang.org/pkg/encoding/json/#Encoder.Encode
Encode writes the JSON encoding of v to the stream, followed by a newline
character.
https://golang.org/pkg/encoding/json/#Decoder.Decode
Decode reads the next JSON-encoded value from its input and stores it in
the value pointed to by v.
character.
https://golang.org/pkg/encoding/json/#Decoder.Decode
Decode reads the next JSON-encoded value from its input and stores it in
the value pointed to by v.
Note that decode makes no promise to consume the extra newline.
On 4 May 2016 at 10:41, Jérôme LAFORGE wrote:
Normally I just read the response with
json.NewDecoder(response.Body).Decode(&something)
My current comprehension, that is json decoder doesn't read the last 0
that indicates the end of chunked response.
or do I miss something?Normally I just read the response with
json.NewDecoder(response.Body).Decode(&something)
My current comprehension, that is json decoder doesn't read the last 0
that indicates the end of chunked response.
>
Do I have to put defer io.Copy(ioutil.Discard, r.Body)?
--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.