title: How to stream http content to a file and a buffer?
body: <p>I want to copy the data from an http response to a file and a buffer.</p>
  
  <p>However, I can't quite figure this out.</p>
  
  <p>Initially I had this:</p>
  
  <pre><code>func DownloadS3(hash string, cluster Cluster, tok *oauth.Token, offset, length int64, f *os.File) ([]byte, error) {
  
      // ... other code not shown ...
  
      resp, err = DoHTTPRequest("GET", s3tok.URL, nil, reqhdr, defaultClientTimeout)
      if err != nil {
          fmt.Println("Error downloading from cluster:", err)
          return nil, err
      }
      defer resp.Body.Close()
  
      if resp.StatusCode != 200 &amp;&amp; resp.StatusCode != 206 {
          return nil, err
      }
  
      // Create buffer to return actual content
      buf := make([]byte, resp.ContentLength)
  
      // This actually does copy the whole downloaded data to the file as
      // expected.  However, I didn't expect io.CopyBuffer to not use the entire
      // buffer.  It ends up returning a portion of the file.
      _, err = io.CopyBuffer(f, resp.Body, buf)
  
      return buf, err
  }
  </code></pre>
  
  <p>So what I want actually is something like</p>
  
  <pre><code>_, err = io.CopyBuffer(f, io.TeeReader(resp.Body, buf), nil)
  </code></pre>
  
  <p>Only, I cant pass buf into TeeReader as it doesn't implement the writer interface.  I'm sure there is a proper method but I can't find it as I fumble
  around looking for an efficient way to do this.</p>
  
  <p>How do I do this without allocating buffer after buffer.  I was trying to be efficient. i.e. It seems silly to write the file and read it back.</p>
  
  <p>What I've tried but doesn't work as expected.</p>
  
  <pre><code>    // Create buffer to return actual content
      buf := make([]byte, resp.ContentLength)
      _, err = io.CopyBuffer(f, io.TeeReader(resp.Body,bytes.NewBuffer(buf)), nil)
      return buf, nil
  </code></pre>