golang break語句能夠break掉select,switch,for

坑,如下例子中,本來覺得close掉done channel後,循環會退出。但事實上會進入死循環golang

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).c#

L:
  for i < n {
      switch i {
      case 5:
          break L // 從L標籤處開始執行
      }
  }
相關文章
相關標籤/搜索