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

Popular posts from this blog

The specified initialization vector (IV) does not match the block size for this algorithm

NodeJS: Error: spawn EINVAL in window for node version 20.20 and 18.20