Preserve bar's ordrer showcase
Vladimir Bauer
8 years ago
| 24 | 24 | for i, url := range [...]string{url1, url2} { |
| 25 | 25 | wg.Add(1) |
| 26 | 26 | name := fmt.Sprintf("url%d:", i+1) |
| 27 | go download(&wg, p, name, url) | |
| 27 | go download(&wg, p, name, url, i) | |
| 28 | 28 | } |
| 29 | 29 | |
| 30 | 30 | p.Stop() |
| 31 | 31 | fmt.Println("Finished") |
| 32 | 32 | } |
| 33 | 33 | |
| 34 | func download(wg *sync.WaitGroup, p *mpb.Progress, name, url string) { | |
| 34 | func download(wg *sync.WaitGroup, p *mpb.Progress, name, url string, n int) { | |
| 35 | 35 | defer wg.Done() |
| 36 | 36 | resp, err := http.Get(url) |
| 37 | 37 | if err != nil { |
| 65 | 65 | ), |
| 66 | 66 | mpb.AppendDecorators(decor.ETA(5, decor.DwidthSync)), |
| 67 | 67 | ) |
| 68 | // Respect the order | |
| 69 | p.UpdateBarPriority(bar, n) | |
| 68 | 70 | |
| 69 | 71 | // create proxy reader |
| 70 | 72 | reader := bar.ProxyReader(resp.Body) |
| 71 | 73 | // and copy from reader |
| 72 | 74 | _, err = io.Copy(dest, reader) |
| 73 | 75 | |
| 74 | if closeErr := dest.Close(); err == nil { | |
| 75 | err = closeErr | |
| 76 | if e := dest.Close(); err == nil { | |
| 77 | err = e | |
| 76 | 78 | } |
| 77 | 79 | if err != nil { |
| 78 | 80 | log.Printf("%s: %v", name, err) |