Aggregating Errors from Parallel Goroutines using Go Channels
Owner: SnippetBot
Created: 2026-07-25 00:00:52
Size: 1.80 KB
Expires: Never
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package main
import (
"errors"
"fmt"
"sync"
"time"
)
// Worker simulates a task that might succeed or fail.
func Worker(id int, shouldFail bool, resultCh chan<- string, errCh chan<- error, wg *sync.WaitGroup) {
defer wg.Done()
fmt.Printf("Worker %d: Starting task.
", id)
time.Sleep(time.Duration(id*100) * time.Millisecond) // Simulate work
if shouldFail {
errCh <- fmt.Errorf("worker %d failed with a simulated error", id)
return
}
resultCh <- fmt.Sprintf("Worker %d completed successfully", id)
fmt.Printf("Worker %d: Finished task successfully.
", id)
}
func main() {
numWorkers := 5
resultChannel := make(chan string, numWorkers)
errorChannel := make(chan error, numWorkers)
var wg sync.WaitGroup
fmt.Println("Launching workers...")
// Launch workers, some will fail
for i := 0; i < numWorkers; i++ {
wg.Add(1)
// Make worker 2 and 4 fail
shouldFail := (i == 1 || i == 3)
go Worker(i+1, shouldFail, resultChannel, errorChannel, &wg)
}
// Wait for all workers to complete
wg.Wait()
// Close channels after all workers are done to signal no more data will come
close(resultChannel)
close(errorChannel)
// Collect results
fmt.Println("
--- Results ---")
for res := range resultChannel {
fmt.Println(res)
}
// Collect errors
fmt.Println("
--- Errors ---")
var collectedErrors []error
for err := range errorChannel {
collectedErrors = append(collectedErrors, err)
fmt.Printf("Collected error: %v
", err)
}
if len(collectedErrors) > 0 {
fmt.Printf("
Total %d errors aggregated.
", len(collectedErrors))
// You might return a multi-error here, or log them.
combinedError := errors.Join(collectedErrors...)
fmt.Printf("Combined error message: %v
", combinedError)
} else {
fmt.Println("No errors reported by workers.")
}
fmt.Println("
Main: All tasks processed.")
}