Build a concurrent file downloader with progress tracking. Applies concepts from Module 12.
Build a concurrent file downloader:
sync.WaitGroup — coordinate workerscontext.Context — for cancellationchannels — for progress updatessemaphore pattern — limit concurrent downloadsfunc worker(ctx context.Context, urls chan string, results chan Result) {
for url := range urls {
select {
case <-ctx.Done():
return
default:
result := download(ctx, url)
results <- result
}
}
}
func download(ctx context.Context, url string) Result {
req, _ := http.NewRequestWithContext(ctx, "GET", url, nil)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return Result{URL: url, Err: err}
}
defer resp.Body.Close()
// Download and track progress...
return Result{URL: url, Size: size}
}
$ cat urls.txt | downloader -workers 5
Downloading 10 files with 5 workers...
[████████████████████] 100% file1.zip (15 MB)
[████████████░░░░░░░░] 60% file2.tar.gz (8/13 MB)
[████░░░░░░░░░░░░░░░░] 20% file3.iso (200/1000 MB)
...
Skills Used: Goroutines, channels, WaitGroups, context cancellation, HTTP streaming, progress tracking.