Convert an io.ReadCloser to String
Using Go, we often make HTTP calls these days using net/http, which result in a response of type io.ReadCloser… which are hard to read for the layman (like me). What we really want to the response in the form of a string which we can read. This post will talk about how to convert these ReadCloser into strings. First we’ll look at the problem, then we have two different solutions.
Example Netlify Function in Go
Netlify, the hosting and web platform, allows you to create “functions” along with their CDN. These functions are hosted on AWS’ Lambda and can be accessible via a URL. So you can create static sites, with extra ability and dynamism (like we use on this site). We wanted to share a post giving an example how to write one of these functions in Go. The aim of the code (below) is to return the version of golang.
Remove All Non-Alphanumeric Characters
We often need to remove symbols and special characters from the strings we’re using (especially with currency!). This post shows how you can keep the letters and numbers, but remove any punctuation, symbols, gammar, etc. For example, if a user types in “$1,000” you can turn it into “1000”. We use the regexp package to do this, first building a regex with .Compile() then running the string through that regex with .
Download a File from a URL
This post shows you how to download a file in Go from the internet to your computer. We use the standard lib http.Get() and io.Copy() functions to help us with it. This function should be efficient as it will stream the data into the file, as aposed to downloading it all into memory, then to file. We also show an alternative below if you want to take the filename from the URL.
Convert HEIC to JPG in Go
The “High Efficiency Image File Format” or HEIF is an image format often used by Apple devices. Although called HEIF, the file types are often heic (presumably the ‘c’ stands for container?) They use a similar encoding method of video formats and are deemed better quality and lower file size than standard jpeg files. In our example, we’re trying to convert these heic files back into jpg files though - so we can use them elsewhere, display them or what ever we choose.
Find the Length of an Array
You can find the length of an array - or to be correct a slice or map - in Go by using the stdlib function len(). We’ve shown some examples below to create both a slice and a map then get the length of it. In Golang, the method is the same for both. Length of Slice We define the entire slice in one go, but you can also use append() to add to the slice.
Sleeping in Golang
How to pause execution and sleep for a number of seconds in Go (golang). We can use the time package from the standard library to sleep for a duration of time. You can use any duration of time, providing you use the constants provided. In our example below we sleep for two seconds, and to illustrate the point, we print out the time before and after we do this. This will only sleep the current goroutine, other parts will continue.
Convert String to Int, Int64, Uint64
This is one of those posts that won’t need a huge introduction. We want to convert a string into an integer. We’ll look at the different ways to do this (based on which sort of int you want). We’ll convert to int, uint and uint64 (though a int64 will be easy enough work out).
Run Golang Tests with Github Actions
Automation is great (right?!) and it can be so easy these days to run your tests automatically on any new pull request or code change. In this post we outline how you can automatically run your Go (golang) test for your project by using Github Actions. The results will then show within Github and is configured using a yaml file. To get this running, you will need to create a workflow file (as shown below).
Docker Setup for Go Development
This is a short and sweet article on running a Go application using Docker. This is a generic example and many use-cases will differ and so have totally different setups. In our examples we’re also using Docker and docker-compose both of which you will need already installed and setup on your machine for this to work.