Get and Set Environment Variables

Environment variables are a useful way to work with sensitive information (think passwords) which your code needs to work with, but don’t need to be checked into your code base. With Go, you can both get and set these details using the os library. package main import ( “fmt” “os” ) func main() { // … Continued

Unzip Files in Go

The code below is used to unzip, or decompress, a zip archive file into it’s constituent files. Because there are likely to be multiple files, it will create the files within a folder (specified with the 2nd parameter). Also, try this code if you are wanting to create zip files. This was based on code … Continued

Create Zip Files in Go

The code below shows how you can create a zip archive after being passed a number of files to compress. This is useful for both bundling files together and creating smaller file sizes. The basics of it are to create the initial zip file then cycle through each file and add it to the archive … Continued

Cross Platform File Paths

Unlike with URLs which have a standardised format (they use forward-slashes to separate), file paths will differ between operating systems. This is mainly a historic issue, as it’s almost inconceivable to change them at this stage. This means Windows will use back-slashes and *nix systems use forward slashes. This is a pain however when writing … Continued

How to Run Go Tests with Coverage Percentage

Go has a brilliant built in testing package, which although it is quite raw, it is very powerful. It also has the ability to show the test coverage as a percentage of the code base. Which is very useful to get perspective of a project and to know perhaps which areas need improvement. You can … Continued

How to check if a string is a URL

Here’s a little snippet to determine if a string is a well structured and considered valid url. This can be useful for pre-empting if a http call will work – or preventing failures from even occurring. In this snippet we’re using a function to tidy this logic and make it reusable. package main import ( … Continued

Updating Go on Ubuntu/Linux

When a new version of Go is released I always want to just jump onto the latest release. Likewise, I’d like the update to be as easy and quick as possible, but often find it harder than expected (I’m used to apt upgrade). Many articles exist online to install Go, less on so on upgrading … Continued

Sleeping in Go – How to Pause Execution

Sleeping, or waiting in Go, is part of the time package. It’s a very simple process, all you need to do is specify the duration to sleep for, which in this cases is a number followed by it’s unit. package main import ( “fmt” “time” ) func main() { fmt.Printf(“Current Unix Time: %v\n”, time.Now().Unix()) fmt.Println(“Sleeping…”) … Continued

Detect if Code is Running On Windows (at Runtime)

On of the advantages of using Go is it’s cross-platform support, the ability to write the code once and have it work across many operating systems. However, there are times when you’ll need to change the route through your code based which OS is running it. If you’re needing to do this on a large … Continued

Get the Current Username, Name and Home Dir (Cross Platform)

In this example we get the current user, the user who is executing the program, and their details. These details include information like user id (Uid), username (the short version of their name), name (the user’s full name) and the user’s home directory location. To do this we use the os/user package. This package also … Continued

Get a URL parameter from a Request

Often it’s important to read off the parameters sent through from the request. If it’s a GET request these will lie within the url itself and can be read using the request URL property. It’s important to note that when getting the parameters in this way it will always return an array (or slice?). package … Continued

Add Line Numbers to Log Output

Many programming languages allow you to print the line number of when and where something has happened. This is very useful for debugging a problem when it has occurred. By default in Go this is off, but you can turn it on by setting flags within the log. package main import “log” func main() { … Continued

Convert io.ReadCloser to a String

Net/http is an amazing package but there are times you need to work with the response of call you have just made. Many functions require a string as input so we have to convert it first by passing it through a buffer. package main import ( “fmt” “net/http” “bytes” ) func main() { response, _ … Continued

How to Install Go in Ubuntu 16.04

Download the latest version of Go. Ubuntu does come with a version of go in it’s apt listings but it won’t be as up-to-date as downloading it directly. Once downloaded we extract it into a folder we can work with. wget https://storage.googleapis.com/golang/go1.8.1.linux-amd64.tar.gz tar xvf go1.8.1.linux-amd64.tar.gz Once downloaded and extracted, move to the folder to /usr/local … Continued