Created
August 19, 2017 02:08
-
-
Save ego008/566376a774b0ad7a70d5521685b6da20 to your computer and use it in GitHub Desktop.
Golang download big file from url
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
package main | |
import ( | |
"fmt" | |
"io" | |
"net/http" | |
"os" | |
"strings" | |
) | |
func downloadFromUrl(url string) { | |
tokens := strings.Split(url, "/") | |
fileName := tokens[len(tokens)-1] | |
fmt.Println("Downloading", url, "to", fileName) | |
// TODO: check file existence first with io.IsExist | |
output, err := os.Create(fileName) | |
if err != nil { | |
fmt.Println("Error while creating", fileName, "-", err) | |
return | |
} | |
defer output.Close() | |
response, err := http.Get(url) | |
if err != nil { | |
fmt.Println("Error while downloading", url, "-", err) | |
return | |
} | |
defer response.Body.Close() | |
n, err := io.Copy(output, response.Body) | |
if err != nil { | |
fmt.Println("Error while downloading", url, "-", err) | |
return | |
} | |
fmt.Println(n, "bytes downloaded.") | |
} | |
func main() { | |
countries := []string{"GB", "FR", "ES", "DE", "CN", "CA", "ID", "US"} | |
for i := 0; i < len(countries); i++ { | |
url := "http://download.geonames.org/export/dump/" + countries[i] + ".zip" | |
downloadFromUrl(url) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment