您好,登錄后才能下訂單哦!
package?csp import?( ????"fmt" ????"sync" ????"testing" ) /*?func?dataProducer(ch?chan?int,?wg?*sync.WaitGroup)?{ ????go?func()?{ ????????for?i?:=?0;?i?<?10;?i++?{ ????????????ch?<-?i ????????} ????????wg.Done() ????}() } func?dataReceiver(ch?chan?int,?wg?*sync.WaitGroup)?{ ????go?func()?{ ????????for?i?:=?0;?i?<?10;?i++?{ ????????????data?:=?<-ch ????????????fmt.Println(data) ????????} ????????wg.Done() ????}() } func?TestChannelClose(t?*testing.T)?{ ????ch?:=?make(chan?int) ????var?wg?sync.WaitGroup ????wg.Add(1) ????dataProducer(ch,?&wg) ????wg.Add(1) ????dataReceiver(ch,?&wg) ????wg.Wait() }?*/ func?dataProducer(ch?chan?int,?wg?*sync.WaitGroup)?{ ????go?func()?{ ????????for?i?:=?0;?i?<?10;?i++?{ ????????????ch?<-?i ????????} ????????close(ch)?//沒有加closepanic了 ????????wg.Done() ????}() } func?dataReceiver(ch?chan?int,?wg?*sync.WaitGroup)?{ ????go?func()?{ ????????for?{ ????????????if?data,?ok?:=?<-ch;?ok?{ ????????????????fmt.Println(data) ????????????}?else?{ ????????????????break ????????????} ????????} ????????wg.Done() ????}() } func?TestChannelClose(t?*testing.T)?{ ????ch?:=?make(chan?int) ????var?wg?sync.WaitGroup ????wg.Add(1) ????dataProducer(ch,?&wg) ????wg.Add(1) ????dataReceiver(ch,?&wg) ????//?wg.Add(1) ????//?dataReceiver(ch,?&wg) ????//?wg.Add(1) ????//?dataReceiver(ch,?&wg) ????wg.Wait() } /*>go??test 0 1 2 3 4 5 6 7 8 9 PASS ok????????0.249s >?*/ /* func?TestChannelClose(t?*testing.T)?{ ????ch?:=?make(chan?int) ????var?wg?sync.WaitGroup ????wg.Add(1) ????dataProducer(ch,?&wg) ????wg.Add(1) ????dataReceiver(ch,?&wg) ????wg.Add(1) ????dataReceiver(ch,?&wg) ????wg.Add(1) ????dataReceiver(ch,?&wg) ????wg.Wait() } >go??test 0 1 4 5 6 7 8 9 3 2 PASS ok????????0.360s >?*/
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。