【退出程序时,防止无缓存的channel没有消费完的方法】教程文章相关的互联网学习教程文章

退出程序时,防止无缓存的channel没有消费完的方法【代码】

参考代码package mainimport("fmt""sync""time""runtime" )var wg sync.WaitGroup// 生产者 func Send(ch chan int){x:=0defer func(){if err:=recover();err!=nil && err.(runtime.Error).Error() == "send on closed channel"{fmt.Println(err)fmt.Println("即将产生的数据:",x)}else{close(ch) //关闭的目的:不在发送数据}wg.Done()}()for i:=0;i<10;i++{x++ch <- x} }// 消费者 func Receive(ch chan int){defer func(){if ...