Skip to content

Instantly share code, notes, and snippets.

@ajbeach2
Created January 22, 2019 23:46
Show Gist options
  • Save ajbeach2/bd1506114f39cd3c965830c048727cc1 to your computer and use it in GitHub Desktop.
Save ajbeach2/bd1506114f39cd3c965830c048727cc1 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"io"
"net/http"
"os"
)
func downloadFile(filepath string, url string) (err error) {
// Create the file
out, err := os.Create(filepath)
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()
// Check server response
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("bad status: %s", resp.Status)
}
// Writer the body to file
_, err = io.Copy(out, resp.Body)
if err != nil {
return err
}
return nil
}
func main(){
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment