func DownloadFile filepath url string error out err os Create filepath

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
func DownloadFile(filepath, url string) error {
out, err := os.Create(filepath)
if (err != nil) {
return err
}
defer out.Close()
// 10 попыток загрузки
for i := 0; i < 10; i++ {
resp, err := http.Get(url)
if (err != nil) {
if err, ok := err.(net.Error); ok && err.Timeout() {
time.Sleep(250 * time.Millisecond)
continue;
}
return err
}
defer resp.Body.Close()
_, err = io.Copy(out, resp.Body)
if (err != nil) {
return err
}
return nil
}
return nil
}