Menu Close


Go: Parallel programming


goroutine is a lightweight thread managed by the Go runtime. A gorutine is a function that is executed using keyword “go”. Having a function “f” we can run the function using “go” keyword:

Goroutines run in the same address space, so access to shared memory must be synchronized. The “sync” package provides useful primitives, although you won’t need them much in Go as there are other primitives: Channels


The “select” statement lets a goroutine wait on multiple communication operations. A “select” blocks until one of its cases can run, then it executes that case. It chooses one at random if multiple are ready.

Default Selection

Basic sends and receives on channels are blocking. However, we can use select with a default clause to implement non-blocking sends, receives, and even non-blocking multi-way selects.



Channels are a typed conduit through which you can send and receive values with the channel operator, <-. The data flows in the direction of the arrow.

Like maps and slices, channels must be created before use using make:

By default, sends and receives block until the other side is ready. This allows goroutines to synchronize without explicit locks or condition variables.

The example code sums the numbers in a slice, distributing the work between two goroutines. Once both goroutines have completed their computation, it calculates the final result.

Closing a channel

A sender can close a channel to indicate that no more values will be sent. Receivers can test whether a channel has been closed by assigning a second parameter to the receive expression: after

When ok is false if there are no more values to receive and the channel is closed.

The loop for i := range c receives values from the channel repeatedly until it is closed.

Note: Only the sender should close a channel, never the receiver. Sending on a closed channel will cause a panic.

Note: Channels aren’t like files; you don’t usually need to close them. Closing is only necessary when the receiver must be told there are no more values coming, such as to terminate a range loop.

Buffered Channels

Channels can be buffered. Provide the buffer length as the second argument to make to initialize a buffered channel:

Sends to a buffered channel block only when the buffer is full. Receives block when the buffer is empty.


The io package specifies the io.Reader interface, which represents the read end of a stream of data.

The Go standard library contains many implementations of these interfaces, including files, network connections, compressors, ciphers, and others. The io.Reader interface has a Read method:


Read populates the given byte slice with data and returns the number of bytes populated and an error value. It returns an io.EOF error when the stream ends. The next example code creates a strings. Reader and consumes its output 8 bytes at a time.


We’ve seen how channels are great for communication among goroutines.

But what if we don’t need communication? What if we just want to make sure only one goroutine can access a variable at a time to avoid conflicts?

This concept is called mutual exclusion, and the conventional name for the data structure that provides it is mutex. Go’s standard library provides mutual exclusion with  sync.Mutex and its two methods: {lock, unlock}

We can define a block of code to be executed in mutual exclusion by surrounding it with a call to  lock and unlock as shown on the Inc method. We can also use “defer” to ensure the mutex will be unlocked as in the “value” method.