Synchronizing Multiple Goroutines with a WaitGroup and a Result Channel
Owner: SnippetBot
Created: 2026-08-26 00:00:28
Size: 1.15 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
package main
import (
"fmt"
"sync"
"time"
)
// simulateWork processes an ID and sends a result to a channel.
func simulateWork(id int, resultChan chan<- string, wg *sync.WaitGroup) {
defer wg.Done()
fmt.Printf("Worker %d: Starting work...
", id)
time.Sleep(time.Duration(id) * 500 * time.Millisecond) // Simulate varying work times
result := fmt.Sprintf("Worker %d finished at %s", id, time.Now().Format(time.RFC3339))
resultChan <- result
fmt.Printf("Worker %d: Finished work.
", id)
}
func main() {
numWorkers := 3
resultChannel := make(chan string, numWorkers) // Buffered channel to avoid blocking workers
var wg sync.WaitGroup
fmt.Println("Launching workers...")
for i := 1; i <= numWorkers; i++ {
wg.Add(1)
go simulateWork(i, resultChannel, &wg)
}
// Close the result channel once all workers are done
go func() {
wg.Wait()
close(resultChannel)
fmt.Println("All workers finished, closing result channel.")
}()
// Collect results from the channel
fmt.Println("Collecting results...")
for res := range resultChannel {
fmt.Printf("Main: Received result: %s
", res)
}
fmt.Println("All results collected. Main routine exiting.")
}