Last active
November 16, 2015 18:47
-
-
Save vessenes/94aace435b7f75c9f6d5 to your computer and use it in GitHub Desktop.
Sample Wait Group usage in go
This file contains 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" | |
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