mirror of
https://github.com/crazybber/awesome-patterns.git
synced 2024-12-01 17:36:09 +03:00
25 lines
421 B
Go
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
|
||
|
}
|