martincartledge.github.io

This is the eleventh entry of my weekly series Learning Go. Last week I talked about Sorting Data in Go. This week I will be talking about how Concurrency works in Go. Before I really dive into the topic of Concurrency, I feel that I need to make some differences between Concurrency and Parallelism since they often times are confused with each other. I will also explain a few pieces of the Go language that allows us to use Concurrency. These pieces are Go Routines and Go Statements.

Concurrency vs. Parallelism

Concurrency

the ability for various parts of a program to run (executed) out-of-order, or in partial order without affecting the final result

Go Routines

Go Statements

Before we jump into how to use Concurrency in Go, I think we should discuss a few of the pillars around writing Concurrent code in Go. A few of these pillars are Goroutines and Channels

Goroutines

a lightweight thread of execution

So, what is a Goroutine and why should I care about them? Here are a few things to consider:

Let’s take a look at an example of using a traditional function (blocking) with a few Goroutines (non-blocking) to better illustrate their place in our Go code

package main

import (
	"fmt"
	"time"
)

func countToFive(wasCalledBy string) {
	for i := 0; i < 5; i++ {
		fmt.Println(wasCalledBy, i)
	}
}

func countToThree(wasCalledBy string) {
	for i := 0; i < 3; i++ {
		fmt.Println(wasCalledBy, i)
	}
}

func main() {
	countToFive("direct - blocking")

	go countToFive("I am a goroutine!")

	go countToThree("I am another goroutine!")

	time.Sleep(time.Second)
	fmt.Println("exit program")
}
// direct - blocking 0
// direct - blocking 1
// direct - blocking 2
// direct - blocking 3
// direct - blocking 4
// using another goroutine! 0
// using a goroutine 0
// using a goroutine 1
// using a goroutine 2
// using a goroutine 3
// using a goroutine 4
// using another goroutine! 1
// using another goroutine! 2

Let’s walk through what is happening:

Quick note: we are importing the time package because we need to wait for a second in order to allow our goroutines to finish. Remember, they are not blocking (synchronous); therefore, we need to wait for them to finish their computations.

We import the time package that we will use in this example just to wait for our Goroutines to finish. I have found it is much more common to use a WaitGroup, we will discuss these later in the post

import (
	"fmt"
	"time"
)

Next, we create two functions, countToFive and countToThree, both of these expect a single parameter wasCalledBy which is of type string.

func countToFive(wasCalledBy string) {
	for i := 0; i < 5; i++ {
		fmt.Println(wasCalledBy, i)
	}
}

func countToThree(wasCalledBy string) {
	for i := 0; i < 3; i++ {
		fmt.Println(wasCalledBy, i)
	}
}

Calling our Goroutine with the wasCalledBy argument will help illustrate how Go executes these Goroutines

Inside of func main I call the countToFive function directly, without making use of a Goroutine

func main() {
	countToFive("direct - blocking")

As the argument says, I am not using the go keyword and creating a Goroutine; therefore, this code will be synchronous and block our thread of execution

On the next line, I create a Goroutine. I do so very easily by calling the same function and placing the go keyword in front of the function identifier

go countToFive("I am a goroutine!")

Next, I fire off another goroutine by placing the go keyword in front of the function identifier.

go countToThree("I am another goroutine!")

In order to ensure that our Goroutines finish, we are using the time package in order for us to sleep for one second.

time.Sleep(time.Second)

What do you expect to see in our logs? What order do you expect these Goroutines to run in?

The output might surprise you, however, I hope it will illuminate some of the power that Goroutines can give you.

// direct - blocking 0
// direct - blocking 1
// direct - blocking 2
// direct - blocking 3
// direct - blocking 4
// using another goroutine! 0
// using a goroutine 0
// using a goroutine 1
// using a goroutine 2
// using a goroutine 3
// using a goroutine 4
// using another goroutine! 1
// using another goroutine! 2

The first 5 lines should not surprise you, we are calling a function without using a Goroutine; therefore, it runs in a synchronous (blocking) manner.

The next few lines should raise some eyebrows, however. Do you notice that our countToThree function logged an item before countToFive did?

This is the power of Goroutines. The Go runtime allows us to write code that can be executed in a concurrent way.

WaitGroups

Using WaitGroups to wait for multiple goroutines to finish is common practice when using Go. WaitGroup is a type which is a part of the sync package.

There are a few functions that come with WaitGroups that you will use often. The most important of these are Add and Done. Let me walk you through how to use these.

package main

import (
	"fmt"
	"sync"
)

func countRoutine(upTo int, wg *sync.WaitGroup) {
	for i := 0; i < upTo; i++ {
		fmt.Println("count routine: ", i)
	}
	wg.Done()
}

func count(upTo int) {
	for i := 0; i < upTo; i++ {
		fmt.Println("count: ", i)
	}
}

func main() {
	var wg sync.WaitGroup

	wg.Add(1)

	go countRoutine(10, &wg)

	count(5)

	wg.Wait()
}

Let’s walk through what is happening, line-by-line:

As I mentioned earlier, since the WaitGroup is a time from the sort package, we need to make sure we import it

import (
	"fmt"
	"sort"
)

Next, we create a function with the identifier countRoutine which has two parameters: upTo of type string and wg which is a pointer to sync.WaitGroup

Note: WaitGroups can only be passed to functions as a pointer

Inside of this function, we create a for loop and iterate until we reach the upTo value that we pass into the function. In order for WaitGroup to know that a goroutine is complete, we run the Done() function

func countRoutine(upTo int, wg *sync.WaitGroup) {
	for i := 0; i < upTo; i++ {
		fmt.Println("count routine: ", i)
	}
	wg.Done()
}

We then create a function with the identifier count with a single parameter upTo of type int. We have the same for loop inside of this function, the only difference is we are not using a WaitGroup because this is not a Goroutine

func count(upTo int) {
	for i := 0; i < upTo; i++ {
		fmt.Println("count: ", i)
	}
}

Inside of the main function, we create a variable using the var keyword and give this variable the identifier wg of type sync.WaitGroup

var wg sync.WaitGroup

In order to tell the Go runtime about our WaitGroup we have to add one. We can do this easily by using the Add() function that takes an argument of type int that signifies how many WaitGroups you would like to add. For this example we only have one Goroutine, so we will just add one:

wg.Add(1)

Next, we use the go keyword to launch countRoutine as a Goroutine and pass 10 as our upTo argument and a WaitGroup pointer (&wg) as our wg argument

go countRoutine(10, &wg)

We call the count function which will be a synchronous, blocking function

count(5)

This might be one of the most important pieces to remember. As you see we are calling a Wait() function on the last line inside of main. This function lets the Go runtime know that we have Goroutines that is not complete yet and to keep our program running.

wg.Wait()

As I mentioned earlier, the way we let the Go runtime know that our Goroutine is complete is by calling the Done() function at the end of our Goroutine. Once we do this, the Go runtime knows it can exit the program.

wg.Done()

In Summary

By using the power of Goroutines paired with the help of WaitGroups, we can write concurrent code in Go. Pretty cool, huh? I have broken up this topic into two pieces because I have a lot more to show you about writing concurrent Go code and the tools that Go gives us to use. Next week I will be talking about Channels, Mutex, and Race Conditions. See you then!