sort.Sort & sort.Stable

- 2 mins read

Go 1.6 made improvements to the Sort function in the sort package. It was improved to make fewer calls to the Less and Swap methods. Here are some benchmarks showing the performance of sort.Sort in Go 1.5 vs 1.6:

Sort []int with Go 1.5
BenchmarkSort_1-4       20000000              67.2 ns/op
BenchmarkSort_10-4      10000000               227 ns/op
BenchmarkSort_100-4       500000              3863 ns/op
BenchmarkSort_1000-4       30000             52189 ns/op

Sort []int with Go 1.6
BenchmarkSort_1-4       20000000              64.7 ns/op
BenchmarkSort_10-4      10000000               137 ns/op
BenchmarkSort_100-4       500000              2849 ns/op
BenchmarkSort_1000-4       30000             46949 ns/op

source: state of go

The Defer Statement

- 3 mins read

The Go programming language has a defer statement that allows for a function call to be run just before the currently running function returns. Here is how the defer statement is explained in the language specification:

“A “defer” statement invokes a function whose execution is deferred to the moment the surrounding function returns, either because the surrounding function executed a return statement, reached the end of its function body, or because the corresponding goroutine is panicking.”

Go Channel Axioms

- 2 mins read

A while ago I was watching a tech talk by Blake Caldwell on Building Resilient Services with Go. In his presentation he had a slide which listed go channel axioms. I have listed his channel axioms here and provided some short code snippets to hopefully clarify them.

A send to a nil channel blocks forever

var ch chan bool
ch <- true // will always block

We declare a channel ‘ch’ but do not initialize it (with make), so it is a nil channel. We then attempt to send a value down the channel, this causes a blocking operation.

dotGo 2015

- 5 mins read

Dot conf. logo

On Monday the 9th of November I was in Paris attending dotGo, the European Go conference. This blog post is a summary of my time there.

Pre-Conference

The day before the conference, the Paris tech talks group organized a pre-conference meetup/party. There were a large number of delegates who turned up and the meet up consisted of about six or seven talks. The talks were about how individuals at there respective companies were using Go. All the projects demoed and talked about were network related i.e. using Go to write a load balancer which solved a particular problem. After the talks, there was a chance to socialize and eat pizza with other gophers :).

tmux Cross Platform Config.

- 1 min read

If like me you use tmux on both Linux and OS X, then managing your tmux configuration can be a pain. The problem is there is configuration that is specific to either OS, such as copy and paste behavior. This blog post will show you how to manage your tmux configuration across platforms in a better way.

The first thing you want to do is create a dot file for each platform, so for Linux create .tmux-linux.conf and for OS X create .tmux-osx.conf. Create and place these files in the same location as your .tmux.conf file (most likely your home directory).

Things to do before committing Go code

- 3 mins read

This blog post will list some of the basic things you should really do before committing Go code into your repository.

1) Run gofmt/goimports

gofmt is probably the most popular Go tool amongst gophers. The job of gofmt is to format Go packages, your code will be formatted to be consistent across your code base. For example if we have the following if statement:

if x == 42 { fmt.Println("The answer to everything") }

Then running gofmt on this will format the code to:

Golang UK Conference 2015

- 1 min read

British Gopher

On Friday the 21st of August I attended the Golang UK conference 2015 held at the amazing Brewery in London. This post is a short write up of my time at the conference.

This was my first ever conference so apart from the talks, I did not know what else to expect. Overall though, I found the conference was excellent and I met a wide range of interesting people.

Gofmt and Rewrite Rules

- 3 mins read

One thing I absolutely love about Go is its tooling support. Whenever I use the numerous tools I always discover something new. In this short post I will be showing off gofmt’s -r flag, this flag allows you to apply a rewrite rule to your source before formatting.

A rewrite rule is a string in the following format:

pattern -> replacement

Both pattern and replacement must be valid Go expressions (more on this later), lets apply a simple rewrite to the following code:

Go and String Concatenation

- 2 mins read

When writing Go code you should try to stay away from concatenating strings using the ‘+’ and ‘+=’’ operators.

Strings in Go, like many other languages (Java, C#, etc…) are immutable, this means after a string has been created it is impossible to change. Here is what the Go Programming Language Specification has to say about the string type:

“A string type represents the set of string values. A string value is a (possibly empty) sequence of bytes. Strings are immutable: once created, it is impossible to change the contents of a string. The predeclared string type is string.”

Calling C from Go

- 4 mins read

This post will show you the basics of how to call C code from a Go package.

Lets get started with an example:

Create a header file “add.h” with a function prototype:

#ifndef _ADD_H_
#define _ADD_H_
int add(int, int);
#endif

Create the source file “add.c” containing the definition for add:

#include "add.h"

int add(int a, int b)
{
   return a + b;
}

Create a Go package “main.go”: