Canonical guidance

Use when

Avoid

Preferred pattern

for {
	select {
	case <-ctx.Done():
		return ctx.Err()
	case item := <-in:
		_ = item
	}
}

Anti-pattern

go func() {
	for item := range in {
		process(item)
	}
}()

Explanation: This anti-pattern is common in rushed concurrency code because the goroutine appears independent, but without cancellation it can outlive the caller and leak work.

Why

Related pages

Sources