Go Concurrency Patterns
Goroutines, channels, select, sync primitives, and common concurrency patterns in Go.
Goroutines, channels, select, sync primitives, and common concurrency patterns in Go.
Go's concurrency constructs and patterns: goroutines, channels, buffered channels, select, WaitGroup, mutexes, worker pools and the context package, each with its keyword, a description, an example and notes.
| Name | Keyword | Description | Example | Notes | Pattern |
|---|---|---|---|---|---|
| Goroutines | go | Lightweight threads managed by Go runtime; cheap to create (few KB stack). | go func() { fmt.Println("running in goroutine") }() | thousands of concurrent goroutines possible; multiplexed onto OS threads | null |
| Channels | chan | Typed conduits for communication between goroutines; synchronous or buffered. | ch := make(chan int); go func() { ch <- 42 }(); val := <-ch | Blocking by default; unbuffered channels synchronize goroutines | null |
| Buffered Channels | make(chan T, n) | Channels with capacity; sender blocks only when buffer full. | bufCh := make(chan string, 5); bufCh <- "msg"; // non-blocking if space | Useful for queue-like patterns; decouples sender/receiver rates | null |
| Select Statement | select | Wait on multiple channel operations; like switch for channels. | select { case msg := <-ch1: fmt.Println(msg) case msg := <-ch2: fmt.Println(msg) case <-time.After(time.Second): fmt.Println("timeout") } | Random choice if multiple ready; default case non-blocking | null |
| WaitGroup | sync.WaitGroup | Wait for a collection of goroutines to finish; Add, Done, Wait. | var wg sync.WaitGroup; wg.Add(2); go func(){ defer wg.Done(); ... }(); wg.Wait() | Must call Done exactly once per Add; typically defer wg.Done() | null |
| Mutex (Mutual Exclusion) | sync.Mutex | Protect shared memory; Lock/Unlock ensure exclusive access. | var mu sync.Mutex; mu.Lock(); counter++; mu.Unlock() | Prefer channels over mutexes when possible (communicating vs sharing) | null |
| Worker Pool Pattern | null | Distribute work across multiple workers; collect results. | jobs := make(chan Job); results := make(chan Result); for w:=1; w<=3; w++ { go worker(jobs, results) }; for j:=range jobs { jobs <- j }; close(results); for r:=range results { ... } | Classic pipeline: distribute tasks, process concurrently, collect | Fan-out / Fan-in |
| Context Package | context.Context | Carry deadlines, cancellation signals, and request-scoped values across API boundaries. | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second); defer cancel(); select { case <-ctx.Done(): fmt.Println("timeout") } | Use for cancellation, timeouts, and passing request-scoped data | null |
The shape of this dataset, read from the data itself: every column, its type and a real value. Agents can use this to plan a query without fetching the whole document.
| Section | Field | Type | Example |
|---|---|---|---|
| concepts | name | string | Goroutines |
| concepts | keyword | string | go |
| concepts | description | string | Lightweight threads managed by Go runtime; chea… |
| concepts | example | string | go func() { fmt.Println("running in goroutine")… |
| concepts | notes | string | thousands of concurrent goroutines possible; m… |
| concepts | pattern | string | Fan-out / Fan-in |
The static files contain the same reference rows as the API, with no API rate limit. Live metadata, such as view counts, may differ. Use the API for content negotiation; use the static files for a cacheable snapshot.
curl "https://yjtoon.com/api/dataset/go-concurrency-patterns?format=toon"
const res = await fetch( "https://yjtoon.com/static-data/dataset/go-concurrency-patterns.toon" ); const toon = await res.text();
Rate limit: 120 requests per minute per IP, no key and no signup. API reference →