awesome-patterns/playground/goroutine/basic_waitgroup_test.go
2018-09-07 21:11:24 +08:00

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")
}