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:
This is a short example on how to convert an integer number into a string. This is a common use case when printing to screen or working with the number as if it was a string. To do this we use the
strconv package and the
When making http requests with Go it is almost always necessary to check the status code of the response which is returned. Generally if the status code is between 200 and 300 you should receive a successful response. But anything except a 200-300 status, we often need to handle.
Go, as a language, makes it very easy to work in parallel on data through it use of go routines (more info on go routines here). One of the tricky parts about using go routines however is a) to know when they are finished and b) to pass data back from the go routine. Channels make both of these things possible. In this post we’re only going to look at a basic use case of channels.