Go 下载文件到本地

Stream + Copy流

func downloadFile(url string, path string) {
  defer wg.Done()

  // Get the data
  resp, err := http.Get(url)
  if err != nil {
    panic(err)
  }
  defer resp.Body.Close()

  // Create output file
  out, err := os.Create(path)
  if err != nil {
    panic(err)
  }
  defer out.Close()

  // copy stream
  _, err = io.Copy(out, resp.Body)
  if err != nil {
    panic(err)
  }
}

 

Leave a Reply

Your email address will not be published. Required fields are marked *