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.
![Learn More](https://www.linux-magazine.com/var/linux_magazin/storage/images/media/linux-magazine-eng-us/images/misc/learn-more/834592-1-eng-US/Learn-More_medium.png)
News
-
NVIDIA Released Driver for Upcoming NVIDIA 560 GPU for Linux
Not only has NVIDIA released the driver for its upcoming CPU series, it's the first release that defaults to using open-source GPU kernel modules.
-
OpenMandriva Lx 24.07 Released
If you’re into rolling release Linux distributions, OpenMandriva ROME has a new snapshot with a new kernel.
-
Kernel 6.10 Available for General Usage
Linus Torvalds has released the 6.10 kernel and it includes significant performance increases for Intel Core hybrid systems and more.
-
TUXEDO Computers Releases InfinityBook Pro 14 Gen9 Laptop
Sporting either AMD or Intel CPUs, the TUXEDO InfinityBook Pro 14 is an extremely compact, lightweight, sturdy powerhouse.
-
Google Extends Support for Linux Kernels Used for Android
Because the LTS Linux kernel releases are so important to Android, Google has decided to extend the support period beyond that offered by the kernel development team.
-
Linux Mint 22 Stable Delayed
If you're anxious about getting your hands on the stable release of Linux Mint 22, it looks as if you're going to have to wait a bit longer.
-
Nitrux 3.5.1 Available for Install
The latest version of the immutable, systemd-free distribution includes an updated kernel and NVIDIA driver.
-
Debian 12.6 Released with Plenty of Bug Fixes and Updates
The sixth update to Debian "Bookworm" is all about security mitigations and making adjustments for some "serious problems."
-
Canonical Offers 12-Year LTS for Open Source Docker Images
Canonical is expanding its LTS offering to reach beyond the DEB packages with a new distro-less Docker image.
-
Plasma Desktop 6.1 Released with Several Enhancements
If you're a fan of Plasma Desktop, you should be excited about this new point release.