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)