Join us

How To Download File With Golang Goroutine/WaitGroup ?


What’s goroutine ?

Go language provides a special feature known as a Goroutines. A Goroutine is a function or method which executes independently and simultaneously in your program.

You are allowed to create multiple goroutines in a single program. You can create a goroutine simply by using go keyword as a prefixing to the function or method like this:

But this code will not print “Hello World” because the `main` func is running on goroutine. So you can use WaitGroup of package `sync` to print word.

In this document, we’ll create a small program to download any file with download link.

So let’s go !!!

Get the repo for this article here.

First generate the project like this:

You can give any name to your project, I decide to name “file-downloader”. Open project in your favorite IDE (vs-code, Golang…). Now create file and name it “main.go”.

In this code, we can see a function (worker) to which we pass a url, and we’ll develop our program here.

We check that the url received is valid and return error or we continue.

We make a request on the url to retrieve the headers.

  • check if url is valid url, else return an error
  • check if header “Accept-Ranges” exists. If not exist, return an error
  • recover file name

Why do we have to check this header (Accept-Ranges) ?

The Accept-Ranges HTTP response header is a marker used by the server to advertise its support for partial requests from the client for file downloads. The value of this field indicates the unit that can be used to define a range.

In the presence of an Accept-Ranges header, the browser may try to resume an interrupted download instead of trying to restart the download (see more…).

In our case the ranges will allow us to split the file to be downloaded into several parts which will be provided to the goroutines.

In this part, we declare variable witch contains the number of part of times we want to split the file, it represent the number of goroutines too. But, for the moment, we have not add the keyword go therefore the application don’t run in goroutine. The ideas is to:

  • Create a file (one part of file)
  • Make a request to download part of file in terms of range
  • Write in part of file

We repeat this three times in the loop because we define nbPart=3 .

Add the keyword go and waitgroups

After the loop for , we create a new file witch will aggragate all files and it’s finished.


You can add a visual of download. Install this package:

Don’t forget to load and import all package. To run the project:

Get the repo for this article here.

Hope you all enjoy it!

Be lenient on my English, I’m French guy. Thanks !!!

Follow to get updates.

Only registered users can post comments. Please, login or signup.

Start blogging about your favorite technologies, reach more readers and earn rewards!

Join other developers and claim your FAUN account now!

User Popularity



Total Hits