When working with any form of http communication in Go you’re going to come across scenarios where you need to know a request’s method - whether it’s a GET, POST, etc. If you’re writing an API with Go you’ll most likely have the incoming request in the form of a http.Request object. This request has Method property which allows you to get the http method in the form of a string.
This is an example of how to convert HTML code into a PDF using AWS’ Lambda service and S3 Triggers. So that once a HTML file is upload to S3 it will automatically be converted into a PDF which should appear in the same bucket shortly after - all using a serverless function. To do this, other than the standard library, we’re using a Go wkhtmltopdf library and the Go AWS sdk.
In the example below we are looking at how to take the first x number of characters from the start of a string. If we know a character we want to separate on, like a space, we can use strings.Split() instead. But for this we’re looking to get the first 6 characters as a new string. To do this we first convert it into a rune, allowing for better support in different languages and allowing us to use it like an array.
Middleware is a term used in many different ways within software development, but we’re referring to it as a way to wrap requests and responses in simple abstracted functions which can be applied to some routes easily. In our example below we’re using an AuthMiddleware to check incoming requests for the correct api key and rejecting them if they don’t. Another good example for this technique is applying headers to responses, if you know the content type of the response, we can set it in middleware.
Generating HMACs (Keyed-Hash Message Authentication Code) are often used as a way of proving data integrity and authenticity. They involve three integrals parts, the algorithm (in our case SHA256), the secret and the data. They a used mainly because data can be checked between two parties without the sharing of the secret. In go, there’s a convenient library to help us out with this called crypto/hmac and we show an example of how to do this below.
In the example below we use the gographics/imagick package as a wrapper to the C library for ImageMagick to convert our PDF into a JPG. The processes goes as follows: We use the package to load in our test file which we then process by setting the resolution, compression levels and alpha channel settings then we save the final output file. Because the library is built on C, it’s important we call the Terminate and Destroy functions appropriately to keep our memory usage in check.
In Go, although errors are a controversial subject, they are in fact very simple. As programmers of Go we also spend a lot of our time writing if err != nil. But we often also need to create these errors and pass them back to other functions to handle. The errors package allows to create errors, as per the error interface, which can be dealt with like any other error. In our example below we handle the error on main() and produce the error in DoSomething().
In Go, we use structs to define and group data, as we would use classes in other OOP languages. However, in Go the naming of attributes within the struct is important because if it starts with a lower-case it’s seen as private and with an upper-case first letter it’s seen as public. We can encode these structs into data formats like json but we might want to rename the fields, struct tags allow us to do this.
We’ve already got a post on converting an integer to a string but the process of converting a variable of type uint64 to a string is a little different. For an int we can use Itoa(), but for an unsigned int 64 we can still use strconv but we can use the FormatUint function instead.
The program below is designed to print out the current state of how much memory is being used, how much has been used and how much the system has reserved. You only really need the PrintMemUsage() function to do this, the rest of the main() is there to illustrate it working (with a gif showing this at the end of this post). The most important figure if often the Alloc which is the amount allocated heap objects.