Finding and retrieving Google Drive files with Go
Reader Without a Type
The fact that a function accepts a reader interface is typical of Go. The function receives an object that can use the Read()
method with which it can consume the data. It does not matter where the data come from – a local file, the web, or a database. In this way, Go, which is so type-strict otherwise, opens itself up to polymorphism, without needing tedious declarations. The consuming function simply calls Read()
and is not in the least interested how the data transpired.
Chunk by Chunk
Wherever data are read and written chunk by chunk, several things can go wrong at any given time, and the programmer has to keep an eagle eye on the process to ensure that the data written to the target file arrive intact (i.e., that the local bytes are 100 percent identical to those on Google Drive).
When loaded off the web, data usually trickle in in small portions. Even a file of several hundred megabytes usually arrives at the downloading application in chunks of no more than 32KB. Since the recipient already knows in advance how large the entire file is going to be from the meta-information provided initially, it only needs to string together the chunks to restore the file on the client side. At the same time, it knows at all times what percentage of the data has already arrived and how much is still outstanding.
Careful When Copying
Listing 4 optimistically creates a buffer of 1MB in data
in line 19, into which the Read()
function drops the next incoming chunk of data. However, incoming packets are typically only 32KB in size; the rest of the buffer remains unoccupied when Read()
returns. The number of bytes actually present is available in the return value count
, and line 28 uses data[:count]
to reduce the length of the byte slice to the actual length of the data by truncating the trailing garbage data.
If the data stream from Google Drive dries up because the end of the file is reached, Read()
returns io.EOF
in line 22 as an error code. But be careful: This does not mean that there's no data left in data
. Instead, count
also again shows you in this case the number of bytes that arrived before the EOF. A client that disregards this last morsel because it thinks that there is nothing left on an EOF will create corrupted download files.
The back end of the copy loop has the writer code, which receives pieces of data from the reader and stores them in a file previously opened for writing. Not only does it have to write the very last bit from the reader into the target file (the bit arrives at the same time as the EOF), but it also has to empty its internal buffers at the very end, by flushing their contents into the target file. If this were left out in line 39, the last few bytes would be missing in the generated PDF document, which causes astounding erratic behavior in some PDF readers. Don't ask me how I know that.
« Previous 1 2 3 4 Next »
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Gnome Fans Everywhere Rejoice for the Latest Release
Gnome 47.2 is now available for general use but don't expect much in the way of newness, as this is all about improvements and bug fixes.
-
Latest Cinnamon Desktop Releases with a Bold New Look
Just in time for the holidays, the developer of the Cinnamon desktop has shipped a new release to help spice up your eggnog with new features and a new look.
-
Armbian 24.11 Released with Expanded Hardware Support
If you've been waiting for Armbian to support OrangePi 5 Max and Radxa ROCK 5B+, the wait is over.
-
SUSE Renames Several Products for Better Name Recognition
SUSE has been a very powerful player in the European market, but it knows it must branch out to gain serious traction. Will a name change do the trick?
-
ESET Discovers New Linux Malware
WolfsBane is an all-in-one malware that has hit the Linux operating system and includes a dropper, a launcher, and a backdoor.
-
New Linux Kernel Patch Allows Forcing a CPU Mitigation
Even when CPU mitigations can consume precious CPU cycles, it might not be a bad idea to allow users to enable them, even if your machine isn't vulnerable.
-
Red Hat Enterprise Linux 9.5 Released
Notify your friends, loved ones, and colleagues that the latest version of RHEL is available with plenty of enhancements.
-
Linux Sees Massive Performance Increase from a Single Line of Code
With one line of code, Intel was able to increase the performance of the Linux kernel by 4,000 percent.
-
Fedora KDE Approved as an Official Spin
If you prefer the Plasma desktop environment and the Fedora distribution, you're in luck because there's now an official spin that is listed on the same level as the Fedora Workstation edition.
-
New Steam Client Ups the Ante for Linux
The latest release from Steam has some pretty cool tricks up its sleeve.