Go For Files Free Download

How to download files in Go

See the release history for more information about Go releases. As of Go 1.13, the go command by default downloads and authenticates modules using the Go module mirror and Go. The 30.14.46 version of GoforFiles is available as a free download on our website. The latest version of the program is supported on PCs running Windows 2000/XP/Vista/7/8/10, both 32 and 64-bit. GoforFiles is included in Internet & Network Tools. This download was scanned by our antivirus and was rated as virus free. A progress bar and upload status for each file is displayed. Multiple file downloads - Download multiple files at once with our Java powered download utility. A progress bar and download status is displayed for each file. Email notification - Option to automatically notify you and/or your users when they have a new file available for downloading. Download Files by Google 1.0.378055542 for Android for free, without any viruses, from Uptodown. Try the latest version of Files by Google 2021 for Android.

The best way to learn a new programming language is to write small utility programs. Let’s write one such program to download files via http in Go.

Download

I am going to show the entire source code first and then walk through each section of it.

Files

Download a file over http

main function

Since this is a command line tool, we start with the package main and function main. We ask the user to pass the url to download and the filename to save it under as command line arguments. We can access them using the os.Args slice.

We just pass the url and filename to the DownloadFile function. The only return value from this function is an error, which we display to the user and exit.

DownloadFile function

Now if we look at the DownloadFile function, this is pretty simple.

  1. We create a new file at the filepath location
  2. Then we use http.Get() to do a GET request to the url.
  3. Finally we use io.Copy() to read from the resp.Body into the newly created file out.
  4. We make sure to close both the file and the response.

Now how can we say that it is memory efficient and doesn’t download and store the entire file in memory? For that we can just look at the source code of io.Copy() and follow along till we reach copyBuffer(). Here we can see that it create a buffer []byte array of size 32kb and uses that to read from the source Reader to destination Writer.

Better DownloadFile

This does work well, but for this tool to be a bit more useful, it needs to show some output to the user. Some indication that it just downloaded a file successfully. Especially if the file is a few hundreds of MB in size, we don’t want the command to keep waiting.

Let’s also add in an extra feature of first downloading to a temporary file so that we don’t overwrite an old file till the new file is completely downloaded.

WriteCounter to count bytes

First off we are going to create a struct which can be used to count the number of bytes which is written. It has only a simple field of uint64. But what is interesting are the two methods that we are going to write for this struct.

Write() and PrintProgress()

Files

We are implementing the Write method for this struct, which makes this object of the io.Writer interface. We can pass this object to any function which requires a io.Writer interface. And the Write function just increments the counter by the size of the bytes written into it and then calls the PrintProgress method.

The PrintProgress method is just an utility method which prints how many bytes were written. It uses the go-humanize package to convert the number of bytes into a human readable number.

DownloadFile function

Files Go For Pc

Now there is just one minor change in the DownloadFile function. Instead of just passing resp.Body into io.Copy, we are creating an io.TeeReader which takes in the counter struct we created earlier.The io.TeeReader, requires one Reader and one Writer, and it returns a Reader by itself. It reads from resp.Body, writes to the counter, and then passes those bytes to the outer io.Copy function. This is a common pattern followed in Unix pipes.

Other than the TeeReader, we are creating a temporary file to download our file, so that we don’t overwrite an existing file before the entire file is downloaded. Once the download is complete, we rename the file names and complete our program.

Go For Files Free Download

If you run the entire code to download a large file, this is how it shows up on the terminal.

Goforfiles Free Download For Pc

The source code of both the programs are available on this gist link.

Conclusion

This shows, how age-old patterns like the unix pipes and tee commands are still relevant and we can build simple tools using those patterns. But more important is the way interfaces are implemented in golang, which allows you to create new structures like the WriteCounter and pass them to any place which takes in an io.Writer interface.