您好,登錄后才能下訂單哦!
坑,以下例子中,原本以為close掉done channel后,循環(huán)會(huì)退出。但事實(shí)上會(huì)進(jìn)入死循環(huán)
for {
select {
case packet := <-s.avPacketChan:
s.sendPacket(packet)
case <-s.done: //終止channel
break
}
}
原因:
[A "break" statement terminates execution of the innermost "for", "switch" or "select" statement.
官方文檔](https://golang.org/ref/spec#Break_statements)
BreakStmt = "break" [ Label ] .
If there is a label, it must be that of an enclosing "for", "switch" or "select" statement, and that is the one whose execution terminates (§For statements, §Switch statements, §Select statements).
L:
for i < n {
switch i {
case 5:
break L // 從L標(biāo)簽處開始執(zhí)行
}
}
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。