Piping Commands into Go Binaries

- 2 mins read

In this post we will see how we can pipe commands into our Go applications. We will write a basic command line program which demonstrates the functionality. Before we get started, it’s worth going over the basics of piping commands.

The modular nature of the UNIX operating system. Allows the user to use basic commands to build up new commands. By allowing the output of one command to be used as the input for the next command. The general form of the pipe command is:

The Go sync.Pool type stores temporary objects, and provides get and put methods. Allowing you to cache allocated but unused items for later reuse. And relieving pressure on the garbage collector.

The purpose of the sync.Pool type is to reuse memory between garbage collections. Which is why sync.Pool is drained during garbage collection (GC).

Here is an example of how to use the sync pool:

package main

import (
	"bytes"
	"fmt"
	"sync"
)

var bufPool = sync.Pool{
	New: func() interface{} {
		return new(bytes.Buffer)
	},
}

func main() {
	b := bufPool.Get().(*bytes.Buffer)
	b.WriteString("What is past is prologue.")
	bufPool.Put(b)
	b = bufPool.Get().(*bytes.Buffer)
	fmt.Println(b.String())
}

Output:

Access Raspberry Pi Externally using ngrok

- 3 mins read

In this blog post we will set up our Raspberry Pi so it will accessed using SSH from outside our home network. Below is a diagram of the architecture:

[ Home Network : Raspberry Pi ] <– [ ngrok ] –> [ External Network ]

From our home network we will create a secure tunnel, through ngrok. Which we will then connect to from our external network. This will allow us to SSH into our Raspberry Pi, and manage it.

Raspberry Pi Slow SSH Fix

- 1 min read

If you SSH into your Raspberry Pi and have noticed a lag when typing characters into the terminal. Then the following fix may get rid of the lag (it worked for me!).

Log into your Raspberry Pi and type:

$ sudo nano /etc/ssh/sshd_config

At the bottom of the config file add:

UseDns no

Save the file, then restart sshd:

$ service ssh restart

Or better, reboot your Raspberry Pi:

$ sudo reboot

SSH’ing into your Raspberry Pi should no longer be slow after these steps. 🤞

Runtime and User Level Panics in Go

- 2 mins read

How can our application code detect the difference between a runtime panic and a user level panic? Before answering this question. Let’s take a look at the difference between a runtime panic, and user level panic.

A runtime panic is one thrown by the Go runtime, there are many things that can trigger a runtime panic. An example of the the most common runtime panic would be attempting to index an array out of bounds. The Go runtime would detect the illegal access and call the built-in panic function.

Creating Empty Git Commits

- 2 mins read

You may not know it, but Git allows you to create an empty commit in to your repo. It doesn’t like it, but you can do it any way.

An empty commit is one where you don’t actually commit any code changes i.e. if you git status your repo, you get the message:

On branch master
nothing to commit, working tree clean

Why would you want to create an empty commit? You may want to communicate changes, which have nothing to do with code. But you have updated something that you want the rest of the team to know about. And you feel that communicating this via the git log make sense.

Changing the Date of Git Commits

- 2 mins read

Have you ever looked at peoples Github contribution timelines and seen a word or some ASCII art. This is done by creating a repo, then manipulating the dates of commits, to create the desired design.

This blog post is not about creating funky pieces of art in your Github commit history timeline. But instead, shows you how to change the date of a commit using Git.

When you make a git commit you can actually set the date of the commit to anything you want. Lets get started.

Using Subtests and Sub-benchmarks in Go

- 4 mins read

In this post we will walk through an example of how to use the new subtests and sub-benchmarks functionality introduced in Go 1.7.

Subtests

One of the nifty features in Go is the ability to write table driven tests. For example, if we wanted to test the function:

func Double(n int) int {
    return n * 2
}

Then we could write a table driven test as follows:

func TestDouble(t *testing.T) {
	testCases := []struct {
		n    int
		want int
	}{
		{2, 4},
		{4, 10},
		{3, 6},
	}
	for _, tc := range testCases {
		got := Double(tc.n)
		if got != tc.want {
			t.Errorf("fail got %v want %v", got, tc.want)
		}
	}
}

Note: The test case {4, 10} is present to make the test fail, 4 * 2 != 10 😃.

SVG Sprites

- 1 min read

Within an single SVG file we can define many sprites. This consists of merging all your SVG sprites into a single .svg image file. Every sprite is wrapped in a ‘symbol’ tag, like this:

<svg class="character" width="100pt" height="100pt" version="1.1" xmlns="http://www.w3.org/2000/svg">
    <symbol id="circle-red" viewBox="0 0 100 100">
      <circle cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="red" />
    </symbol>
    <symbol id="circle-black" viewBox="0 0 100 100">
      <circle cx="50" cy="50" r="40" stroke="black" stroke-width="3" />
    </symbol>
</svg>

We can then use HTML or CSS to pick out each part of the image:

Updating Third Party Packages in Go

- 1 min read

Just a short post on how to update packages using go get.

To update all third party packages in your GOPATH use the following command:

go get -u all

To update a specific package, just provide the full package name to go get:

go get -u github.com/gorilla/mux

What about vendor-ed packages? These are updated in exactly the same way as above:

go get -u my-project/vendor/megacorp/foo

If you want more information about your GOPATH, run the command: