Skip to content

Instantly share code, notes, and snippets.

@dhanush
Last active January 20, 2022 08:36
Show Gist options
  • Save dhanush/a82220c307194d53084c2aec7bf6dafc to your computer and use it in GitHub Desktop.
Save dhanush/a82220c307194d53084c2aec7bf6dafc to your computer and use it in GitHub Desktop.
func downloadMultipleFiles(urls []string) ([][]byte, error) {
done := make(chan []byte, len(urls))
errch := make(chan error, len(urls))
for _, URL := range urls {
go func(URL string) {
b, err := downloadFile(URL)
if err != nil {
errch <- err
done <- nil
return
}
done <- b
errch <- nil
}(URL)
}
bytesArray := make([][]byte, 0)
var errStr string
for i := 0; i < len(urls); i++ {
bytesArray = append(bytesArray, <-done)
if err := <-errch; err != nil {
errStr = errStr + " " + err.Error()
}
}
var err error
if errStr!=""{
err = errors.New(errStr)
}
return bytesArray, err
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment