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

Passing Arguments into your 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

Attach a Logger to your Router

If you’re working with the net/http package, you can easily create a router to pass different http calls to different functions. A logger allows you to keep track of these calls. In this example, we just log the call to the terminal (or stdout). To use this logger wrap any handlers you have with Logger. … Continued

JSON Decode into Objects

This is a way of taking a JSON string and forming an object array with it. These objects can then be used and manipulated as you please. This uses the json encode as well just to demonstrate it works. The primary function used to process the json is the json.Unmarshal method. package main import ( … Continued

JSON Encode an Array of Objects

This is how to convert any object within go, into the JSON data structure. In our example we’re using an array of Page objects, each with their own properties and encoding them. We’re then just printing this string to Stdout, but if we’re using the net/http to create an api, we would want to write … Continued