码迷,mamicode.com
首页 > 其他好文 > 详细

golang break语句可以break掉select,switch,for

时间:2018-07-19 11:56:09      阅读:238      评论:0      收藏:0      [点我收藏+]

标签:for   退出   label   etc   https   lang   min   nts   循环   

坑,以下例子中,原本以为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标签处开始执行
      }
  }

golang break语句可以break掉select,switch,for

标签:for   退出   label   etc   https   lang   min   nts   循环   

原文地址:http://blog.51cto.com/xwandrew/2147090

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!