func main(){ ch := make(chan int,1000) go func() { for i :=0;i<1000;i++{ ch <- i } // 关闭channel close(ch) }() // 使用range来处理channel里面的数据 // 当channel关闭时,range方式会将里面剩余的数据全部读取完成,在退出 for x := range ch{ fmt.Println(x) } } func main(){ m1 := make(chan bool) go func() { for{ select { case <-m1: fmt.Println("close========================") return case <-time.After(time.Millisecond*10000): fmt.Println("-----") } } }() // 控制 goroutine 并发的数量 limit := make(chan bool,2) wg := sync.WaitGroup{} for i :=0;i<10;i++{ limit <- true wg.Add(1) go func(i int) { defer func() { <- limit wg.Done() }() //HandleLogic() fmt.Println(i) }(i) } wg.Wait() m1 <- true close(m1) }