Continuing with our lockdown learning of Go language. Here’s a video where we Play more with WaitGroup in Golang
package main
import (
"fmt"
"sync"
)
func main() {
wg := sync.WaitGroup{} //a structure
fmt.Println(wg)
wg.Add(1)
//go doSomething(wg) //Passing a copy of waitgroup. So no modifications to the original
go doSomething(&wg)
//wg.Add(1) //Wait for 1 go routine to finish
//wg.Add(2)//Wait for 2 go routines to finish. But we have only one GoRoutine. hmm
//This delta: <number> is equal to the number of times Done() gets called from goroutines
//go func() {
// fmt.Println("...Inside goroutine 1")
// wg.Done()
// wg.Done() //Silly isn't it?
// wg.Done()//panic: sync: negative WaitGroup counter
//
//}()
wg.Wait()
fmt.Println("****END")
}
func doSomething(wg *sync.WaitGroup) {
fmt.Println("...DoSomething")
wg.Done()
}
You can watch other videos in Go lang in the links below