Home > Backend Development > Golang > How Can Goroutines and WaitGroups Be Used to Download Multiple Files Concurrently in Golang?

How Can Goroutines and WaitGroups Be Used to Download Multiple Files Concurrently in Golang?

Mary-Kate Olsen
Release: 2024-10-26 07:22:03
Original
393 people have browsed it

How Can Goroutines and WaitGroups Be Used to Download Multiple Files Concurrently in Golang?

Downloading Multiple Files in Parallel with Goroutines in Golang

In this article, we'll explore how to utilize goroutines in Golang to download multiple files concurrently from a remote location.

Consider the following code snippet, which attempts to download files from Dropbox:

<code class="go">package main

import (
    "encoding/json"
    "fmt"
    "io"
    "io/ioutil"
    "net/http"
    "net/url"
    "os"
    "path/filepath"
    "sync"
)

...
func main() {

    ...
    for i, file := range flr.FileList {

        go download_file(file, tr)

        if i >= 2 {
            break
        }
    }
   ...
}</code>
Copy after login

However, upon attempting to execute this code using goroutines, it fails. This is because the main goroutine exits before the goroutines responsible for downloading files have completed their tasks.

To address this issue and ensure that the main goroutine waits for the goroutines to complete before exiting, we can employ a sync.WaitGroup, as follows:

<code class="go">package main

import (
    ...
    "sync"
)

...
func main() {

    ...
    var wg sync.WaitGroup
    for i, file := range flr.FileList {
        wg.Add(1)

        go download_file(file, tr, &wg)

        if i >= 2 {
            break
        }
    }
    wg.Wait()
    ...
}</code>
Copy after login

Here, we create a sync.WaitGroup and use it to keep track of the number of goroutines that have not yet completed their tasks. This allows the main goroutine to wait until all the goroutines have completed their tasks before exiting.

In the modified download_file function, we call wg.Done() to signal the completion of the goroutine, decrementing the wg counter.

By utilizing goroutines and a Waitgroup, we can effectively download multiple files concurrently, improving the overall efficiency of the download process.

The above is the detailed content of How Can Goroutines and WaitGroups Be Used to Download Multiple Files Concurrently in Golang?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template