Skip to content

Instantly share code, notes, and snippets.

@vessenes
Last active November 16, 2015 18:47
Show Gist options
  • Save vessenes/94aace435b7f75c9f6d5 to your computer and use it in GitHub Desktop.
Save vessenes/94aace435b7f75c9f6d5 to your computer and use it in GitHub Desktop.
Sample Wait Group usage in go
package main
import "fmt"
import "sync"
import "time"
func main() {
var wg sync.WaitGroup
wg.Add(2)
go func() {
fmt.Println("Starting slow func")
time.Sleep(3 * time.Second)
wg.Done()
}()
go func() {
fmt.Println("Starting fast func")
wg.Done()
}()
for i := 0; i < 10; i++ {
wg.Add(1)
go func(i int) {
fmt.Println("Goroutine", i, " here")
wg.Done()
}(i)
}
fmt.Println("Waiting")
wg.Wait()
fmt.Println("Done")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment