Concurrency patterns

Contents
Worker pool
Fan-out results
Cancel together
Keep output order

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

Related articles
Golang
Go course: zero to hero in 50 lessons
What is Go and where it runs
Install Go and check the version
Your first Go program
Modules with go mod
Variables
Constants
Types
Numbers
Strings
Conditions with if
Switch in Go
Loops with for
Arrays in Go
Slices in Go
Maps in Go
Functions in Go
Errors as values
Pointers in Go
Structs in Go
Methods in Go
Interfaces in Go
Embedding instead of inheritance
Generics in Go
Packages and imports
The go toolchain
Testing with go test
Advanced errors
defer in Go
Strings, bytes and runes
Time in Go
JSON
Files and IO
HTTP servers
HTTP clients
Context
Goroutines
Channels
select and sync
CLI: args, flags, env
Logging
Regular expressions
Sorting
Concurrency patterns
Databases with database/sql
Benchmarks and profiling
Toolchain and CI
Project layout
Capstone: wordfreq CLI
Capstone: JSON API server
Hero roadmap: the whole course on one page
Install Go on Windows 11
Install Go on Ubuntu
Install Go on Rocky Linux
Install Go on macOS
Install Go on FreeBSD
Go in Docker

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.ru
Write to info@urn.su, info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: