This repository demonstrates how to use Go's sync.WaitGroup
to synchronize goroutines and handle errors that may occur during their execution. The example showcases how to capture and process errors from multiple goroutines using a channel.
- This example demonstrates using a `sync.WaitGroup` with error handling in Go.
- It includes a worker function that simulates tasks, where one worker intentionally encounters an error.
- The example shows how to propagate errors back to the main goroutine using a channel and handle them appropriately after all workers finish execution.
package main
import (
"fmt"
"sync"
)
func worker1(id int, wg *sync.WaitGroup, errors chan error) {
defer wg.Done()
// Simulate an error for worker 2
if id == 2 {
errors <- fmt.Errorf("worker %d encountered an error", id)
return
}
fmt.Printf("Worker %d completed successfully\n", id)
}
func main() {
// WaitGroup with Error Handling
// we demonstrate using a WaitGroup with error handling in the workers
var wg sync.WaitGroup
errors := make(chan error, 3)
for i := 1; i <= 3; i++ {
wg.Add(1)
go worker1(i, &wg, errors)
}
// Wait for all workers to finish
wg.Wait()
close(errors)
// Check for errors
for err := range errors {
if err != nil {
fmt.Println("Error:", err)
}
}
fmt.Println("All workers completed.")
}
- Make sure you have Go installed. If not, you can download it from here.
- Clone this repository:
git clone https://github.com/Rapter1990/go_sample_examples.git
- Navigate to the
023_waitgroup
directory:
cd go_sample_examples/023_waitgroup/004_waitgroup_with_error_handling
- Run the Go program:
go run 004_waitgroup_with_error_handling.go
When you run the program, you should see the following output:
Worker 3 completed successfully
Worker 1 completed successfully
Error: worker 2 encountered an error
All workers completed.