Chapters & contents Lesson 7 of 12
3 min read
GoExpanded reading

Goroutines and channel coordination

Give concurrent work a clear owner, communication path, and stopping rule.

What you’ll learn

  • Explain a goroutine
  • Use a channel to transfer a result
  • Identify a goroutine that cannot finish

Concurrent does not mean simultaneous

A goroutine is a concurrently executing function managed by the Go runtime. Starting one lets the program coordinate overlapping activities, but does not guarantee that every activity executes simultaneously on separate processors. Begin with the reason for concurrency: waiting for independent I/O, keeping a component responsive, or dividing suitable work. A program with more concurrent activities also has more possible orderings, which must be understood and tested rather than assumed harmless.

Communicate a result explicitly

A channel provides a typed communication mechanism between goroutines. The timing of a send or receive depends in part on whether the channel is buffered and whether another participant is ready. For a first example, use an unbuffered channel to transfer one computed result. Trace which goroutine sends, which receives, and what allows each operation to complete. Then change the example deliberately and explain how the coordination changes.

go · Worked example
package main

import "fmt"

func main() {
    result := make(chan int)
    go func() { result <- 6 * 7 }()
    fmt.Println(<-result) // 42
}

Count work as well as results

Suppose a caller starts three workers and intends to collect three results. If one worker exits early without sending or signalling failure, a caller expecting exactly three receives may wait indefinitely. Returning only the happy-path value is therefore an incomplete protocol. Represent failure and cancellation, and decide which component knows when no more values can arrive. Channel closure is a signal about future sends, so responsibility for closing should be part of the design.

Bound lifetime and resource use

A goroutine that waits forever can retain resources even if it appears inexpensive. Starting a new goroutine for every item in an unbounded input can also exhaust useful capacity. Bound the amount of concurrent work and give each activity an exit path. Context cancellation is commonly used in APIs for propagating cancellation and deadlines, but code must actually observe it. Trace shutdown and failure paths with the same care as successful results.

Test the protocol through behavior

Try an empty workload, one result, a worker failure, and a caller that stops waiting early. Ask which goroutines remain after each case. Avoid adding arbitrary sleeps as proof that work completed; use explicit synchronization or completion signals. When shared memory is involved, use the language’s concurrency tools and development checks appropriately. Channels do not automatically make unrelated accesses to a shared map or slice race-free.

For example, make one of three controlled workers return an error and verify that the collector terminates with that error while the other workers receive a documented shutdown signal.

Try it yourself

A caller expects three results but one worker can fail before sending. What needs to change?

Show a possible solution

Define a protocol that accounts for both success and failure and has a clear completion/cancellation rule. Do not leave the caller waiting for a value that no participant can send.

Check your understanding

Try 11 questions across go. Read each explanation and revisit anything that needs another pass.

Practise this subject

Go to the source

Use these primary references to check details and explore the topic further.

Saved in this browser. Signed-in progress syncs to your account.