awesome-patterns/playground/goroutine/basic_chan_test.go

25 lines
421 B
Go
Raw Permalink Normal View History

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