A goroutine cannot return an error to its caller. The go statement fires and forgets: it starts execution on a separate scheduling path, and the function's return values are discarded. If something goes wrong inside the goroutine, the error has nowhere to go unless you build a path for it explicitly.
This comes up all the time. Concurrent code that fetches data, writes to storage, or calls external services needs to report failure just as much as sequential code does. If it doesn't, you get silent failures: the goroutine hits an error, swallows it, and the caller receives a partial or empty result with no sign that anything went wrong.
The two standard ways to solve this are the result struct and the separate error channel, and this article covers both.
The anti-pattern: silent failure
First, the problem itself. This code looks reasonable but silently drops errors:
func fetchAll(urls []string) []string { results := make([]string, len(urls)) var wg sync.WaitGroup for i, url := range urls { wg.Add(1) go func(i int, url string) { defer wg.Done() resp, err := http.Get(url) if err != nil { return // error is silently dropped } defer resp.Body.Close() body, _ := io.ReadAll(resp.Body) results[i] = string(body) }(i, url) } wg.Wait() return results}If any http.Get fails, results[i] stays at its zero value (an empty string), and fetchAll returns a slice with holes in it. The caller can't tell a successful empty response from a network error: the function completes, the caller moves on, and the error is gone.
Pattern 1: the result struct
In most cases the simplest solution is a struct that holds both the result and the error, sent through a single channel. Each goroutine sends exactly one value, either a result with a nil error or a zero result with a non-nil error, and the caller loops over the channel to collect them.
type fetchResult struct { url string body []byte err error}func fetchAll(ctx context.Context, urls []string) []fetchResult { ch := make(chan fetchResult, len(urls)) for _, url := range urls { go func(url string) { req, err := http.NewRequestWithContext( ctx, http.MethodGet, url, nil, ) if err != nil { ch <- fetchResult{url: url, err: err} return } resp, err := http.DefaultClient.Do(req) if err != nil { ch <- fetchResult{url: url, err: err} return } defer resp.Body.Close() body, err := io.ReadAll(resp.Body) ch <- fetchResult{url: url, body: body, err: err} }(url) } results := make([]fetchResult, 0, len(urls)) for range urls { results = append(results, <-ch) } return results}The channel is buffered to len(urls), so each goroutine can send its result without blocking regardless of when the others finish. The for range urls loop collects exactly as many values as were sent, one per goroutine, so the channel drains completely and the function returns only after all goroutines have finished.
The caller then iterates over the results and handles successes and failures separately:
results := fetchAll(ctx, urls)for _, r := range results { if r.err != nil { log.Printf("failed to fetch %s: %v", r.url, r.err) continue } process(r.url, r.body)}There's one channel, one type and one loop, and every goroutine's outcome reaches the caller, whether it succeeded or failed.
Pattern 2: separate error channel
The alternative is two channels, one for results and one for errors, with the caller selecting over both. You'll see this pattern when the result type is already defined and you don't want to wrap it in a new struct, or when the error-handling logic really is separate from the result-processing logic.
func fetchAll( ctx context.Context, urls []string,) (<-chan []byte, <-chan error) { results := make(chan []byte, len(urls)) errs := make(chan error, len(urls)) for _, url := range urls { go func(url string) { req, err := http.NewRequestWithContext( ctx, http.MethodGet, url, nil, ) if err != nil { errs <- err return } resp, err := http.DefaultClient.Do(req) if err != nil { errs <- err return } defer resp.Body.Close() body, err := io.ReadAll(resp.Body) if err != nil { errs <- err return } results <- body }(url) } return results, errs}The caller must drain both channels. A common approach is to collect a known number of responses using a counter:
results, errs := fetchAll(ctx, urls)for i := 0; i < len(urls); i++ { select { case body := <-results: process(body) case err := <-errs: log.Printf("fetch error: %v", err) }}The select alternates between the two channels, processing whichever is ready first. After len(urls) iterations, all goroutines have sent exactly one message to one of the two channels, and both are fully drained.
Comparing the two patterns
Both patterns propagate errors correctly. The choice between them is mostly about API clarity and how the calling code wants to consume the output.
| Result struct | Separate channels | |
|---|---|---|
| Channels | One | Two |
| Result-error coupling | Always together | Decoupled |
| Caller loop | Single range or for | select over two channels |
| Returned type | Custom struct | Existing types unchanged |
| Ordering | Arbitrary, but explicit via field | Arbitrary, context lost |
The result struct is usually the better default because it's harder to misuse. The caller receives a value that contains both the outcome and the context for that outcome, and there's no second channel to forget to drain, so no goroutine is left blocked.
The separate error channel makes sense when:
- The result type is already defined and adding a wrapper struct would pollute the API
- Errors and results really are processed by different parts of the code
- You are building a pipeline where errors fan in from multiple stages and a dedicated error channel fits naturally into the design
In either case the rule is the same: every goroutine that can fail must have a guaranteed path for that failure to reach the caller. Silent failures show up in any language that combines concurrency with ignored errors, and in Go you use channels to make every failure explicit.