Answers for "download file in go"

Go
0

download file in go

func downloadFile(path string, url string) (error) {

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

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

  // Write the body to file
  _, err = io.Copy(out, resp.Body)
  if err != nil { return err }

  return nil
}
Posted by: Guest on August-31-2021

Browse Popular Code Answers by Language