Most efficient way to convert io.ReadCloser to byte array
<p>I have a very simple Go webserver. It's job is to receive an inbound json payload. It then publishes the payload to one or more services that expect a byte array. The payload doesn't need to be checked. Just sent over.</p>

<p>In this case, it receives an inbound job and sends it to Google PubSub. It might be another service - it doesn't really matter. <strong>I'm trying to find the most efficient way to convert the object to a byte array without first decoding it.</strong></p>

<p>Why? Seems a bit wasteful to decode and convert to JSON on one server, only to unmarshal it later. Plus, I don't want to maintain two identical structs in two packages.</p>

<p>How is it possible to convert the io.ReadCloser to a byte array so I only need to unmarshal once. I tried something like this answer but don't think that's the most efficient way either:</p>

<p><a href="https://stackoverflow.com/questions/9644139/from-io-reader-to-string-in-go">From io.Reader to string in Go</a></p>

<p>My http server code looks like this:</p>

<pre><code>func Collect(d DbManager) http.HandlerFunc {
    return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    w.Header().Set("Content-Type", "application/json; charset=utf-8")

    code := 422
    obj := Report{}
    response := Response{}
    response.Message = "Invalid request"

    decoder := json.NewDecoder(r.Body)
    decoder.Decode(&amp;obj)

    if obj.Device.MachineType != "" {
        msg,_ := json.Marshal(obj)
        if d.Publish(msg, *Topic) {
          code = 200
        }
        response.Message = "Ok"
    }

    a, _ := json.Marshal(response)
    w.WriteHeader(code)
    w.Write(a)
    return
})
}
</code></pre>