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.6.2.linux-amd64.tar.gz tar xvf go1.6.2.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)) }

Write data to a CSV file

CSV is a highly accepted data language, commonly used by Excel and spreadsheets, and as such is very useful if your script is producing data. In the example, we’re forming a data variable, just to illustrate the example, by creating a multi-dimensional array of strings. We are then having to create a file (result.csv) for … Continued

Pass Arguments into you Application

Passing arguments in your application can be especially useful when you’re creating tools which can do different things, or the same thing in a different way. In go, you can do with by declaring your arguments as flags. In our example we’re using an argument to determine whether we should run task A or task … Continued

Writing to File

Often it’s very useful being able to write to a file from a script, for logging purposes, saving results or as a data store. This is a little snippet on how to write to a file. We use the os package to create the file, exiting the program and logging if it can’t do this … Continued

Using Constants

Using constants can often be more efficient than using variables where possible because any references to the constant will be replaced at compile time, where as a variable would have memory allocated to it and can be changed. Using constants if very similar however to using variables and can be declared as a block outside … Continued

Get Unix Time

This is a simple way go getting the current system time in the unix format and in this example we’re just printing the result to screen. If you want to use the returned int as a string, you can pass it into the strconv.FormatInt() method. package main import ( “fmt” “time” ) func main() { … Continued