Created
July 4, 2017 12:58
-
-
Save mjudeikis/b19589b57d5a2fc8ceb076bcd97bd18d to your computer and use it in GitHub Desktop.
http serv
This file contains 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