mirror of
https://github.com/crazybber/awesome-patterns.git
synced 2024-11-29 16:36:05 +03:00
33 lines
564 B
Go
33 lines
564 B
Go
|
package goroutine
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"sync"
|
||
|
"testing"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
func TestWaitGophers(t *testing.T) {
|
||
|
var gophers = []string{"tom", "peter", "john", "brown"}
|
||
|
|
||
|
var waitGroup sync.WaitGroup
|
||
|
waitGroup.Add(len(gophers))
|
||
|
|
||
|
for i := 0; i < len(gophers); i++ {
|
||
|
go func(wg *sync.WaitGroup, name string) {
|
||
|
sleepyGopherSnore(name)
|
||
|
wg.Done()
|
||
|
}(&waitGroup, gophers[i])
|
||
|
}
|
||
|
|
||
|
waitGroup.Wait()
|
||
|
|
||
|
fmt.Println("All done")
|
||
|
}
|
||
|
|
||
|
func sleepyGopherSnore(name string) {
|
||
|
fmt.Println(name, ": ... start snore")
|
||
|
time.Sleep(2 * time.Second)
|
||
|
fmt.Println(name, ": ... snore")
|
||
|
}
|