Concurrency patterns

Worker pool

A worker pool starts a fixed number of goroutines that read tasks from one channel. The number of workers bounds the parallel work, while the channel feeds them evenly.

A WaitGroup tracks the workers, close shuts the task channel when input ends, and Wait blocks main until every worker drains the channel and calls Done.

package main import ( "fmt" "sync" ) func main() { jobs := make(chan int) var wg sync.WaitGroup wg.Add(2) for i := 0; i < 2; i++ { go func(id int) { for job := range jobs { fmt.Println(id, job*job) } wg.Done() }(i) } for n := 1; n <= 5; n++ { jobs <- n } close(jobs) wg.Wait() }

Lines may arrive in any order because two workers print at the same time. One possible run prints the squares of one through five across both workers.

0 1

0 4

1 9

0 16

1 25

Fan-out results

Fan-out means several workers read from one task channel and send answers into one result channel. The shape fits jobs that split cleanly, such as doubling numbers or fetching pages.

The main goroutine knows the task count, so it reads exactly that many answers. A separate goroutine feeds the tasks and closes the channel, which lets every worker exit its range loop on its own.

package main import ( "fmt" ) func main() { jobs := make(chan int) out := make(chan int) for i := 0; i < 3; i++ { go func() { for n := range jobs { out <- n * 2 } }() } go func() { for n := 1; n <= 6; n++ { jobs <- n } close(jobs) }() for i := 0; i < 6; i++ { fmt.Println(<-out) } }

The six doubled values always appear, but their order depends on scheduling. One possible run prints them from smallest to largest.

2

4

6

8

10

12

Cancel together

A done channel broadcasts a stop signal to many goroutines at once. Closing the channel wakes every receiver, because a receive on a closed channel proceeds immediately.

A select statement waits on several channels and runs the first branch that becomes ready. The loop below prints ticks until the done channel closes, then prints one final line and returns.

package main import ( "fmt" "time" ) func main() { done := make(chan struct{}) go func() { time.Sleep(10 * time.Millisecond) close(done) }() tick := time.Tick(3 * time.Millisecond) for { select { case <-done: fmt.Println("stopped") return case t := <-tick: fmt.Println("tick") } } }

The exact tick count depends on timing. A typical run prints a few ticks and then stops on schedule.

tick

tick

stopped

Keep output order

Parallel workers finish in random order, yet many programs must print answers in the original sequence. The fix is to tag every task with its index and place each answer into a result slice at that index.

Each goroutine sends a small struct with the index and the value. Main reads every answer and stores it by index, so the final slice always matches the input order.

package main import ( "fmt" ) type result struct { id int v int } func main() { nums := []int{5, 1, 4} out := make([]int, len(nums)) res := make(chan result) for id, n := range nums { go func(id, n int) { res <- result{id: id, v: n * 10} }(id, n) } for range nums { r := <-res out[r.id] = r.v } fmt.Println(out) }

[50 10 40]

Next: Databases with database/sql

Article author: Arthur Isaev