I have the opposite problem as this question.
In that question, the user asks about decoding a large incoming JSON array.
But, how would I encode a large outgoing JSON array?
For example, I have a http.Handler
like this.
enc := json.NewEncoder(resp)
for obj := range objectChannel {
enc.Encode(obj)
}
However, this doesn't work because it ends up sending invalid JSON to the JavaScript client.
Do I have to manually fix the syntax? For example:
enc := json.NewEncoder(resp)
fmt.Fprint(resp, "[")
for obj := range objectChannel {
enc.Encode(obj)
fmt.Fprint(resp, ",") // and account for the last item
}
fmt.Fprint(resp, "]")
Or is there a better way?