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

25 lines
421 B
Go

package goroutine
import (
"fmt"
"testing"
"time"
)
func TestFiveGopherWithChan(t *testing.T) {
c := make(chan int)
for i := 0; i < 5; i++ {
go sleepyGopherWithChan(i, c)
}
for i := 0; i < 5; i++ {
gopherID := <-c
fmt.Println("gopher ", gopherID, " has finished sleeping")
}
}
func sleepyGopherWithChan(id int, c chan int) {
time.Sleep(2 * time.Second)
fmt.Println("... ", id, " snore ...")
c <- id
}