# Concurrency Patterns

## Worker Pool

Process jobs concurrently with a limited number of workers.

```go
func worker(id int, jobs <-chan int, results chan<- int) {
    for j := range jobs {
        results <- j * 2
    }
}

func main() {
    jobs := make(chan int, 100)
    results := make(chan int, 100)

    for w := 1; w <= 3; w++ {
        go worker(w, jobs, results)
    }

    for j := 1; j <= 5; j++ {
        jobs <- j
    }
    close(jobs)

    // Collect results...
}
```

## Pipeline

Chain of processing stages.

```go
func gen(nums ...int) <-chan int {
    out := make(chan int)
    go func() {
        for _, n := range nums {
            out <- n
        }
        close(out)
    }()
    return out
}

func sq(in <-chan int) <-chan int {
    out := make(chan int)
    go func() {
        for n := range in {
            out <- n * n
        }
        close(out)
    }()
    return out
}
```

## ErrGroup Pattern

```go
g, ctx := errgroup.WithContext(ctx)

g.Go(func() error {
    return fetchOrders(ctx, orderCh)
})

g.Go(func() error {
    return fetchProducts(ctx, productCh)
})

if err := g.Wait(); err != nil {
    return fmt.Errorf("parallel fetch failed: %w", err)
}
```
