title: In Go, how do I write a streaming http response body to a seek position in a file effectively?
body: <p>I have a program that combines multiple http responses and writes to the respective seek positions on a file. I am currently doing this by </p>
  
  <pre><code>client := new(http.Client)
  req, _ := http.NewRequest("GET", os.Args[1], nil)
  resp, _ := client.Do(req)
  defer resp.Close()
  reader, _ := ioutil.ReadAll(resp.Body) //Reads the entire response to memory
  //Some func that gets the seek value someval
  fs.Seek(int64(someval), 0)
  fs.Write(reader)
  </code></pre>
  
  <p>This sometimes results in a large memory usage because of the <code>ioutil.ReadAll</code>. </p>
  
  <p>I tried <code>bytes.Buffer</code> as</p>
  
  <pre><code>buf := new(bytes.Buffer)
  offset, _ := buf.ReadFrom(resp.Body) //Still reads the entire response to memory.
  fs.Write(buf.Bytes())
  </code></pre>
  
  <p>but it was still the same. </p>
  
  <p>My intention was to use a buffered write to the file, then seek to the offset again, and to continue write again till the end of stream is received (and hence capturing the offset value from buf.ReadFrom). But it was also keeping everything in the memory and writing at once. </p>
  
  <p>What is the best way to write a similar stream directly to the disk, without keeping the entire content in buffer?</p>
  
  <p>An example to understand would be much appreciated.</p>
  
  <p>Thank you.</p>