Reading HTTP client response twice

This is what I actually do behind the scene:

  1. Send a HTTP request to a remote server.
  2. Do two things with the HTTP response coming from the remote server.
    1- Read it, parse it to struct and give it to my client in handler.
    2- Dump whole response (uri, headers and body …) into a (because I am auditing all the responses):
    a) TXT file, if the application is running in local env.
    b) S3 bucket, if the application is not running in local env.

I just didin’t want a and b delay me from responding to my client because both sometimes take long time. That’s why I used defer.

@Dean_Davidson So I guess what you are saying is that:

  1. instead of using defer, use it like go audit.Log(...) and manage DATA RACE which is happening at the moment.
  2. stream it rather than using io.ReadAll(res.Body). I guess you mean var buf bytes.Buffer - io.Copy(&buf, resp.Body)?

By the way if you can think of any better solution, I am happy to listen. Thank you.