When running a Go program in the terminal, your program could receive a signal interrupt from the OS for any number of reasons. One of which is if the user presses Ctrl+C on their keyboard (or whatever your operating system/terminal is set to). We can execute some code when this interrupt is received, mainly to clean up and reset what we were working on. In our example we use a goroutine to listen for the interrupt from signal.
We’ve already covered basic downloading of files - this post goes beyond that to create a more complete downloader by including progress reporting of the download. This means if you’re pulling down large files you are able to see how the download’s going. In our basic example we pass the response body into io.Copy() but if we use a TeeReader we can pass our counter to keep track of the progress.
This example shows how to download a file from the web on to your local machine. By using io.Copy() and passing the response body directly in we stream the data to the file and avoid having to load it all into the memory - it’s not a problem with small files, but it makes a difference when downloading large files. We also have an example of downloading large files with progress reports.
In this basic example, we check to see if a file exists before interacting with it (otherwise something’s not going to work as expected). We leverage the power of the os standard library and first use the Stat() function which although it’s usually used to get information about a file, we’re only looking at the errors. We can’t just check for err == nil because any number of errors could be returned so we pass it to IsNotExists() to confirm that it’s an error because the file does not exist.
As programmers we often need to take a string and replace parts of it with something else. The code has three examples, first of which is a basic ‘find all’ and replace, the second changes only the first occurance of ‘sound’ and finally the third example demonstrates how to change a string containing quotes to use escaped quotes. All this functionality is managed by the strings package and the Replace function.
For many different reasons, there will be times when you need to keep data within a cookie to be sent with subsequent requests or read by the recipient. We can do this with Go’s standard library, or by using a package like gorilla’s session, but for this simple example we’ll use the standard library. We’ve created a function called addCookie() which takes a name/key and a value to writes it directly against the ResponseWriter w.
We have a similar post on writing data to a CSV file. This post however, focuses on the simple task of taking data from a csv file and converting it into data we can work with. The first part it to open the file, then we read it into the lines variable and finally we loop through the lines and we turn them into CsvLine objects - obviously in a real scenario we should use a more descriptive naming.
The example shows how to upload a local file onto an S3 bucket using the Go AWS SDK. It’s quite straight forward once you have your session setup and we’ve create an AddFileToS3 function which can be called multiple times when wanting to upload many files. Within the PutObjectInput you an specify options when uploading the file and in our example we show how you can enable AES256 encryption on your files (when at rest).
Random numbers in computing can be useful for many reasons (we won’t go into them too much here though). With Go, they’re simple enough to generate providing you first set a unique as possible seed first. Without setting a seed first, the random number generating will return the same number the first time you run it. In our example, we want to generate a random number somewhere between two other numbers - we use Intn to help with this.
Ever wanted to know the exact type name of the variable you’re using? The Printf is capable of print exactly this information back out to you, like so: