Skip to content

Instantly share code, notes, and snippets.

@ego008
Created August 19, 2017 02:08
Show Gist options
  • Save ego008/566376a774b0ad7a70d5521685b6da20 to your computer and use it in GitHub Desktop.
Save ego008/566376a774b0ad7a70d5521685b6da20 to your computer and use it in GitHub Desktop.
Golang download big file from url
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