Skip to content

Instantly share code, notes, and snippets.

@armando-couto
Last active March 2, 2022 02:44
Show Gist options
  • Save armando-couto/825a45195da94f25a26384be06a57178 to your computer and use it in GitHub Desktop.
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…
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