awesome-patterns/playground/goroutine/basic_waitgroup_test.go

33 lines
564 B
Go
Raw Normal View History

2018-09-07 16:11:24 +03:00
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")
}