Sleeping with Go

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

Serve Static Assets using the Mux Router

Using a router is great when passing off to functions to handle and return data. Often though, you just want to serve an entire directory. This is useful for images, styles and javascript. With this we’re using the Gorilla mux router (“HTTP request multiplexer”) and setup a new route for the entire directory. package main … Continued

Fizz-Buzz Test in Go

A simple program, often used in interviews to show people who can’t code. Prints the numbers from 1 to 100, except if the number is divisible by 3 print fizz or if the number is divisible by 5 print buzz. package main import “fmt” func main() { for i := 1; i

MySQL Database Insert & Get Last Insert ID

This shows how we can use the go-sql-driver (which uses the database/sql interface) to form a connection to our database server using the Open method. Once we have our connection, we can create a statement of our SQL query, and to prevent SQL injection we send the parameters (values which should correspond with to the … Continued

Multi-line Strings

This is an especially useful technique when working with large strings like SQL, HTML or XML within Go. Instead of using either the double quote (“) or single quote symbols (‘), instead use back-ticks to define the start and end of the string. You can then wrap it across lines. If you indent the string … Continued

Anonymous Functions

Here’s a basic example of how an anonymous function, or lambda function, can be used with go. We’re just printing a statement to screen, but it can be used for various things – one of which could be just to segment code which will only need to get run once and doesn’t need to be … Continued

Basic Go Routines (like Threading)

This script basically just prints out the numbers 1 to 10, but it does so by spinning off each print statement into it’s own thread, using a go routine. You’ll notice the script also has a start and an end print statement, but because go routines are used, the end statement will be printed before … Continued

Find the Length of an Array/Slice

With Go, you can find the length of an array (or more accurately a slice) by using the internal len() function. Our example shows us creating a slice and then printing out it’s length. package main import “fmt” func main() { array := []int{1, 2, 3, 4, 5} fmt.Printf(“%d\n”, len(array)) }