package main import ( "fmt" "log" "net/http" "os" ) func GetFilePath(url string) (string, error) { return "image.jpg", nil } func Download(urls []string) (downloaded, failed []string) { for _, url := range urls { filepath, err := GetFilePath(url) if err != nil { failed = append(failed, url) log.Debugf("Can't get file path") } err = DownloadFile(filepath, url) if err != nil { failed = append(failed, url) } downloaded = append(downloaded, url) } } func main() { // download images and print results urls := []string{ "https://static.vinepair.com/wp-content/uploads/2019/10/HopTake-Oct17-Header.jpg", "https://static.turbosquid.com/Preview/001209/767/QP/3D-model-beer-pack-corona_Z.jpg", "https://static.turosquid.com/Preview/001209/767/QP/3D-model-beer-pack-corona_Z.jpg", } downloaded, failed := Download(urls) fmt.Printf("Downloaded: %v\n Failed: %v", downloaded, failed) } func DownloadFile(filepath string, url string) error { resp, err := http.Get(url) if resp != nil { defer resp.Body.Close() } if err != nil { log.Errorf("Can't get file with the following url %s due the error: %s", url, err) return err } out, err := os.Create(filepath) if err != nil { log.Errorf( "Can't create file by the following path %s due the error: %s", filepath, err) return err } defer out.Close() _, err = io.Copy(out, resp.Body) if err != nil { log.Errorf( "Can't write file content for %s url by the following %s path due the error: %s", url, filepath, err) return err } return nil }