package main
import (
"log"
"sync"
"time"
)
func main() {
wg := sync.WaitGroup{}
ch := make(chan int, 3)
n := 3
wg.Add(n)
for i := 0; i < n; i++ {
go func(id int) {
for v := range ch {
log.Println(id, "->", v)
time.Sleep(time.Millisecond)
}
wg.Done()
}(i)
}
for i := 0; i < 100; i++ {
ch <- i
}
close(ch)
wg.Wait()
}
网友评论