From d97d95ed2995188726d63945ae73a0a14e29149b Mon Sep 17 00:00:00 2001 From: gutmet Date: Tue, 12 Feb 2019 00:17:21 +0100 Subject: [PATCH] method to download to file --- goutil.go | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/goutil.go b/goutil.go index f8aac3b..30bce51 100644 --- a/goutil.go +++ b/goutil.go @@ -5,6 +5,7 @@ import ( "errors" "flag" "fmt" + "io" "io/ioutil" "net/http" "os" @@ -312,7 +313,7 @@ func Notify(head string, body string) error { return nil } -func Download(url string) ([]byte, error) { +func DownloadAll(url string) ([]byte, error) { resp, err := http.Get(url) if err != nil { return nil, errors.New("Download: " + err.Error()) @@ -324,3 +325,18 @@ func Download(url string) ([]byte, error) { } return data, err } + +func DownloadToFile(f string, url string) error { + resp, err := http.Get(url) + if err != nil { + return errors.New("DownloadToFile: " + err.Error()) + } + defer resp.Body.Close() + file, err := os.Create(f) + if err != nil { + return errors.New("DownloadToFile: " + err.Error()) + } + defer file.Close() + _, err = io.Copy(file, resp.Body) + return err +}