package main
import (
"fmt"
"sync"
)
func main() {
var wg sync.WaitGroup
print5 := func(c *chan int, i int) {
defer wg.Done()
for j := i; j < i+5; j++ {
*c <- j
}
}
for i := 1; i < 75; i += 5 {
wg.Add(1)
ch := make(chan int, 5)
go print5(&ch, i)
for j := 0; j < 5; j++ {
fmt.Println(<-ch)
}
}
wg.Wait()
}
输出如下,
有疑问加站长微信联系(非本文作者)