Last active
March 2, 2022 02:44
-
-
Save armando-couto/825a45195da94f25a26384be06a57178 to your computer and use it in GitHub Desktop.
WaitGroup is a great way to wait for a set of concurrent operations to complete when you either don’t care about the result of the concurrent operation, or you have other means of collecting their results. If neither of those conditions are true, I suggest you use channels and a select statement instead. WaitGroup is so useful, I’m introducing i…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"sync" | |
"time" | |
) | |
func main() { | |
var wg sync.WaitGroup | |
wg.Add(1) | |
go func() { | |
defer wg.Done() | |
fmt.Println("1st goroutine sleeping...") | |
time.Sleep(1) | |
}() | |
wg.Add(1) | |
go func() { | |
defer wg.Done() | |
fmt.Println("2nd goroutine sleeping...") | |
time.Sleep(2) | |
}() | |
wg.Wait() | |
fmt.Println("All goroutines complete.") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment