Created
September 13, 2015 15:02
-
-
Save Lyubaev/dd74558ca14995cab200 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func HTTPDownload(uri string) ([]byte, error) { | |
| fmt.Printf("HTTPDownload From: %s.\n", uri) | |
| res, err := http.Get(uri) | |
| if err != nil { | |
| log.Fatal(err) | |
| } | |
| defer res.Body.Close() | |
| d, err := ioutil.ReadAll(res.Body) | |
| if err != nil { | |
| log.Fatal(err) | |
| } | |
| fmt.Printf("ReadFile: Size of download: %d\n", len(d)) | |
| return d, err | |
| } | |
| func WriteFile(dst string, d []byte) error { | |
| fmt.Printf("WriteFile: Size of download: %d\n", len(d)) | |
| err := ioutil.WriteFile(dst, d, 0444) | |
| if err != nil { | |
| log.Fatal(err) | |
| } | |
| return err | |
| } | |
| func DownloadToFile(uri string, dst string) { | |
| fmt.Printf("DownloadToFile From: %s.\n", uri) | |
| if d, err := HTTPDownload(uri); err == nil { | |
| fmt.Printf("downloaded %s.\n", uri) | |
| if WriteFile(dst, d) == nil { | |
| fmt.Printf("saved %s as %s\n", uri, dst) | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment