title: What's an efficient way to pass a response body (response.Body) in Go?
body: <p>If I have some code, like the example below, that fetches an image from a link and then saves it to disk, what is the best way to pass around the image data?</p>
  
  <p>I thought about using <code>ioutil.ReadAll(res.Body)</code> to convert it to <code>[]byte</code> but passing that around seems expensive, although I can't tell from the documentation whether or not it returns a slice or an array. I also tried returning a pointer to <code>res.Body</code>, a <code>*io.ReadCloser</code> type, but I couldn't figure out how to properly call the <code>.Close()</code> method on the pointed to interface.</p>
  
  <p>I understand that moving the save code into the <code>FetchImage</code> function would probably be the easiest way to solve this but I would like to have these pieces separate if possible.</p>
  
  <pre><code>type ImageData struct {
      Data      io.ReadCloser
      Name      string
  }
  
  func FetchImage(url string) (io.ReadCloser, error) {
      res, err := http.Get(url)
      if err != nil {
          return nil, err
      }
      return res.Body, nil
  }
  
  func Save(data *ImageData) error {
      defer data.Data.Close()
      file, err := os.Create(data.Name)
      defer file.Close()
      if err != nil {
          return err
      }
      _, err = io.Copy(file, data.Data)
      if err != nil {
          return err
      }
      return nil
  }
  
  func main() {
      body, err := fetcher.FetchImage("https://imgur.com/asdf.jpg")
      if err != nil {
          panic(err)
      }
      imageData := ImageData{body, "asdf.jpg"}
      saver := Saver{config.BaseDir, 1}
      err = saver.Save(&amp;imageData)
      if err != nil {
          panic(err)
      }
  }
  </code></pre>
  
  <p>Additionally, I am very new to Go so if there's anything in this code that looks bad please let me know.</p>