Go (golang) Tutorials - Concurrency Waitgroups

Published: 16 April 2018
on channel: Ambasoft Java
976
19

#golang #go #concurrency #waitgroups

Concurrency - WaitGroups
---------------------------
will wait until a collection of goroutines completes their work
Wait groups will block the main routine from execution until all the go routines finishes their work

waidemo.go
-----------
package main
import ("fmt"
"time"
"sync"
)
func display(i int,g *sync.WaitGroup){
fmt.Println("Start",i)
time.Sleep(2*time.Second)
fmt.Println("End",i)
g.Done() //Specify the waitgroup about the completion of a goroutine
}
func main(){
var g sync.WaitGroup
//use for loop {
g.Add(1) //Will create a waitgroup entry for every go routine invocation
go display(i,&g)
}
g.Wait() //Blocking all to ensure main goroutine waits until all the go routines completed
fmt.Println("Exiting Main goroutine")
}


Watch video Go (golang) Tutorials - Concurrency Waitgroups online, duration hours minute second in high quality that is uploaded to the channel Ambasoft Java 16 April 2018. Share the link to the video on social media so that your subscribers and friends will also watch this video. This video clip has been viewed 976 times and liked it 19 visitors.