March 2019
Beginner to intermediate
324 pages
7h 17m
English
The concept of wait groups is very important for building production level software in Go; it allows you to wait for multiple goroutines to finish before you proceed with the rest of your code.
To fully grasp the benefit of wait groups, let's go back to an earlier code sample:
package mainimport ( "fmt" "time")func runLoopSend(n int, ch chan int) { for i := 0; i < n; i++ { ch <- i } close(ch)}func runLoopReceive(ch chan int) { for { i, ok := <-ch if !ok { break } fmt.Println("Received value:", i) }}func main() { myChannel := make(chan int) go runLoopSend(10, myChannel) go runLoopReceive(myChannel) time.Sleep(2 * time.Second)}
In the preceding code, we had to make the main goroutine sleep for two seconds in order to wait for the ...
Read now
Unlock full access