This repository demonstrates how to use a Go WaitGroup in conjunction with anonymous functions to synchronize multiple goroutines. It showcases how to launch goroutines with anonymous functions and wait for their completion using a sync.WaitGroup
.
- This example demonstrates the use of WaitGroups with anonymous functions in Go.
- It covers launching multiple goroutines, each using an anonymous function to simulate a task, and synchronizing their completion using a `sync.WaitGroup`.
- The example shows how to pass arguments to anonymous functions within goroutines.
package main
import (
"fmt"
"sync"
)
func main() {
// WaitGroup with Anonymous Functions
// Demonstrates using a WaitGroup with an anonymous function
var wg sync.WaitGroup
n := 5
for i := 1; i <= n; i++ {
wg.Add(1)
go func(id int) {
defer wg.Done()
fmt.Printf("Worker %d is processing\n", id)
}(i)
}
// Wait for all workers to finish
wg.Wait()
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/002_waitGroup_with_anonymous_functions
- Run the Go program:
go run 002_waitGroup_with_anonymous_functions.go
When you run the program, you should see the following output:
Worker 3 is processing
Worker 2 is processing
Worker 5 is processing
Worker 4 is processing
Worker 1 is processing
All workers completed.