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.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Patterns in the Archive

    To help him check his Google Drive files with three different pattern matchers, Mike builds a command-line tool in Go to maintain a meta cache.

  • Perl: Google Drive

    Armed with a Chinese guillotine and a scanner with an automatic document feeder, Mike Schilli gives his books some special treatment, courtesy of Google Drive, which offers 5GB of storage space – room enough to start an online PDF collection.

  • Perl: Spotify

    For a monthly fee, the Spotify streaming service beams music onto your desktop or phone. To intensify the groove, Perlmeister Mike Schilli archived his Spotify playlists for eternity using an OAuth-protected web API.

  • Programmatically change YouTube metadata

    Instead of manually editing the metadata of YouTube movies, video craftsman Mike Schilli dips into YouTube’s API spell book and lets a script automatically do the work.

  • Programming Snapshot – Driving Data

    A connector plugged into the diagnostic port of Mike Schilli's cars sends current information such as speed, acceleration, and fuel economy via the mobile phone network to a cloud service. An app and a programmable API read out the data and provide stunning visualizations.

comments powered by Disqus
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