golang goroutine with waitgroup
Waitgroup provides a simple and easy to use approach to managing go routines. A basic example can be shown here.
package main
import (
"fmt"
"sync"
)
func worker(id int, w *sync.WaitGroup) {
defer w.Done()
fmt.Printf("Worker: %d processing tasks \n", id)
}
func main() {
var wg sync.WaitGroup
const numTask = 2
// starting our worker
for i := 0; i < numTask; i++ {
wg.Add(1)
fmt.Printf("firing task %d \n", i)
go worker(i, &wg)
}
wg.Wait()
fmt.Println("Done")
}
Comments