Lesson 16 of 20 · Part 5: Packages and concurrency
Goroutines and channels
Go goroutines and channels for beginners. Run functions concurrently, wait with sync.WaitGroup, send and receive on channels, buffered channels, close, range, select and timeouts.
5 min read · Updated · Beginner friendly
What you'll learn
- Start goroutines and wait for them with sync.WaitGroup
- Send and receive values over channels
- Use buffered channels, close and range
- Wait on several channels at once with select and add timeouts
Imagine downloading ten web pages. Doing them one after another means a lot of waiting: each request spends most of its time waiting for the network. It would be much faster to start all ten and handle each as it finishes. Running several tasks during the same period of time is called concurrency, and it’s where Go really shines.
Concurrency vs parallelism
- Concurrency is about structure: a program is made of tasks that can make progress independently, such as a web server handling many requests.
- Parallelism is about execution: tasks literally run at the same instant on different CPU cores.
Go lets you write concurrent code easily, and the Go runtime runs it in parallel across all your CPU cores automatically.
Goroutines
A goroutine is a function running concurrently with the rest of your program. Start one by putting go in front of a function call:
package main
import "fmt"
func main() {
go fmt.Println("hello from a goroutine")
fmt.Println("hello from main")
}hello from mainWhere did the goroutine’s message go? When main returns, the program ends immediately, and any goroutines still running are stopped. The goroutine probably never got the chance to print. (Run it a few times and you might occasionally see both lines; the timing isn’t guaranteed.)
Goroutines are very cheap. Each starts with a few kilobytes of memory, so a program can run hundreds of thousands of them. They are not operating-system threads; the Go runtime schedules them onto a small number of threads for you.
Waiting with sync.WaitGroup
To wait for goroutines to finish, use a sync.WaitGroup. It’s a counter: Add increases it, Done decreases it, and Wait blocks until it reaches zero:
package main
import (
"fmt"
"sync"
"time"
)
func download(name string, wg *sync.WaitGroup) {
defer wg.Done() // tell the WaitGroup we're finished, even if we return early
time.Sleep(100 * time.Millisecond) // pretend to do slow work
fmt.Println("downloaded", name)
}
func main() {
start := time.Now()
var wg sync.WaitGroup
for _, file := range []string{"a.txt", "b.txt", "c.txt"} {
wg.Add(1)
go download(file, &wg)
}
wg.Wait()
fmt.Printf("all done in about %dms\n", time.Since(start).Round(100*time.Millisecond).Milliseconds())
}downloaded c.txt
downloaded a.txt
downloaded b.txt
all done in about 100msEach download takes 100ms, but all three run at the same time, so the total is about 100ms, not 300ms. The order of the “downloaded” lines changes between runs: goroutines run independently, and there’s no guarantee which finishes first.
Pass the WaitGroup by pointer (*sync.WaitGroup); a copy would have its own separate counter.
Channels: communicating between goroutines
Goroutines often need to send results back. Go’s answer is the channel: a typed pipe that one goroutine sends values into and another receives them from.
package main
import "fmt"
func main() {
messages := make(chan string) // a channel of strings
go func() {
messages <- "ping" // send
}()
msg := <-messages // receive: waits until a value arrives
fmt.Println(msg)
}pingmake(chan T)creates a channel for values of typeT.ch <- vsendsvinto the channel.v := <-chreceives a value from the channel.
With this kind of channel (unbuffered), a send waits until another goroutine receives, and a receive waits until another goroutine sends. That waiting is useful: it synchronises the two goroutines. Here main can’t print before the goroutine has sent its message.
Sharing the work, collecting the results
Here two goroutines each add up half of a slice and send their partial sums back:
package main
import "fmt"
func sum(nums []int, out chan<- int) {
total := 0
for _, n := range nums {
total += n
}
out <- total
}
func main() {
nums := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
results := make(chan int)
half := len(nums) / 2
go sum(nums[:half], results)
go sum(nums[half:], results)
a, b := <-results, <-results // receive two values
fmt.Println("total:", a+b)
}total: 55The parameter type chan<- int means “a channel this function may only send to”. Likewise <-chan int is receive-only. These directional types document intent, and the compiler enforces them.
Go's motto
“Don’t communicate by sharing memory; share memory by communicating.” Instead of several goroutines changing the same variable, pass data between them over channels, so only one goroutine owns it at a time.
Buffered channels
A channel can have a buffer. Sends only wait when the buffer is full, and receives only wait when it’s empty:
package main
import "fmt"
func main() {
queue := make(chan string, 3) // room for 3 values
queue <- "first"
queue <- "second" // no receiver needed yet: there's room in the buffer
fmt.Println("queued:", len(queue), "capacity:", cap(queue))
fmt.Println(<-queue)
fmt.Println(<-queue)
}queued: 2 capacity: 3
first
secondChannels are first in, first out: values come out in the order they went in. Start with unbuffered channels; add a buffer only when you have a reason, such as letting a fast producer get a little ahead of a slower consumer.
Closing a channel and ranging over it
When a sender has no more values, it can close the channel. Receivers can then use for ... range to read every value until the channel is closed and empty:
package main
import "fmt"
func countdown(from int, out chan<- int) {
for i := from; i > 0; i-- {
out <- i
}
close(out) // no more values
}
func main() {
ch := make(chan int)
go countdown(5, ch)
for n := range ch { // stops when ch is closed
fmt.Print(n, " ")
}
fmt.Println("liftoff!")
v, ok := <-ch // receiving from a closed channel never blocks
fmt.Println(v, ok)
}5 4 3 2 1 liftoff!
0 falseRules worth remembering:
- Only the sender should close a channel, never the receiver.
- Sending on a closed channel panics.
- Receiving from a closed channel returns the zero value immediately, with
ok == false. - You don’t have to close every channel; close it when receivers need to know that no more values are coming.
Pipelines
Channels connect naturally into pipelines: stages that each take values in, do something, and pass results on:
package main
import "fmt"
func generate(nums ...int) <-chan int {
out := make(chan int)
go func() {
for _, n := range nums {
out <- n
}
close(out)
}()
return out
}
func square(in <-chan int) <-chan int {
out := make(chan int)
go func() {
for n := range in {
out <- n * n
}
close(out)
}()
return out
}
func main() {
for result := range square(generate(1, 2, 3, 4)) {
fmt.Println(result)
}
}1
4
9
16Each stage runs in its own goroutine, so all stages work at the same time, like an assembly line.
select: waiting on several channels
select waits on several channel operations and runs whichever one is ready first. Combined with time.After, which returns a channel that receives a value after a delay, it gives you timeouts:
package main
import (
"fmt"
"time"
)
func fetch(delay time.Duration) <-chan string {
ch := make(chan string, 1)
go func() {
time.Sleep(delay) // simulate a slow network call
ch <- fmt.Sprintf("response after %v", delay)
}()
return ch
}
func main() {
for _, delay := range []time.Duration{20 * time.Millisecond, 500 * time.Millisecond} {
select {
case res := <-fetch(delay):
fmt.Println(res)
case <-time.After(200 * time.Millisecond):
fmt.Println("timed out after 200ms")
}
}
}response after 20ms
timed out after 200msThe fast call wins against the 200ms timer; the slow call loses. Notice the channel in fetch has a buffer of 1. If nobody is listening anymore after a timeout, the goroutine can still send its value and finish, instead of waiting forever.
A select with a default case doesn’t wait at all: if no channel is ready, default runs immediately. That’s how you do a non-blocking send or receive.
Deadlocks
If every goroutine is waiting and nothing can ever happen, Go detects it and stops the program:
package main
func main() {
ch := make(chan int)
ch <- 1 // nobody will ever receive this
}fatal error: all goroutines are asleep - deadlock!When you see this, look for a send without a receiver, a receive without a sender, or a range over a channel that is never closed.
Exercises
1. Square them all. Start one goroutine per number in []int{2, 3, 4}. Each sends its number squared on a channel. In main, receive three results and print their sum.
Show solution
package main
import "fmt"
func main() {
nums := []int{2, 3, 4}
results := make(chan int)
for _, n := range nums {
go func() {
results <- n * n
}()
}
total := 0
for range nums {
total += <-results
}
fmt.Println("sum of squares:", total)
}sum of squares: 29Since Go 1.22 each loop iteration has its own n, so it’s safe to use n inside the goroutine.
2. Producer and consumer. Write a producer goroutine that sends the words of "go makes concurrency simple" on a channel and closes it. The consumer in main ranges over the channel and prints each word in upper case.
Show solution
package main
import (
"fmt"
"strings"
)
func main() {
words := make(chan string)
go func() {
for _, w := range strings.Fields("go makes concurrency simple") {
words <- w
}
close(words)
}()
for w := range words {
fmt.Println(strings.ToUpper(w))
}
}GO
MAKES
CONCURRENCY
SIMPLE3. First answer wins. Two “servers” answer after 30ms and 80ms. Send both requests at once and print only the first answer, using select.
Show solution
package main
import (
"fmt"
"time"
)
func server(name string, delay time.Duration, out chan<- string) {
time.Sleep(delay)
out <- name
}
func main() {
answers := make(chan string, 2) // buffered so the slower server never blocks
go server("mirror-1", 80*time.Millisecond, answers)
go server("mirror-2", 30*time.Millisecond, answers)
select {
case first := <-answers:
fmt.Println("fastest:", first)
case <-time.After(time.Second):
fmt.Println("no answer")
}
}fastest: mirror-2Recap
go f()runsfin a new goroutine;mainreturning ends the whole program.sync.WaitGroupwaits for a group of goroutines:Add,Done,Wait.- Channels send typed values between goroutines; unbuffered channels also synchronise them.
- Close a channel to signal “no more values”;
rangereads until it’s closed. selectwaits on several channels;time.Afteradds timeouts.
Next: what happens when goroutines share data, and the patterns that keep it safe.
Finished this lesson?
Your progress is saved in this browser only.