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 +}