1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| package main
import "fmt"
func test(jobs chan int, done chan bool) { for { j, more := <-jobs if more { fmt.Println("received job", j) } else { fmt.Println("received all jobs") done <- true return } } }
func sendMsg(jobs chan int){ for j := 1; j <= 3; j++ { jobs <- j fmt.Println("sent job", j) } close(jobs) fmt.Println("sent all jobs") }
func main() { jobs := make(chan int, 5) done := make(chan bool)
go test(jobs, done) go sendMsg(jobs) <-done }
|