坑,以下例子中,原本以爲close掉done channel後,循環會退出。但事實上會進入死循環
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標籤處開始執行
}
}