Does a break statement break from a switch/select?

后端 未结 7 1239
不知归路
不知归路 2020-12-04 07:57

I know that switch/select statements break automatically after every case. I am wondering, in the following code:

for {
    switch          


        
相关标签:
7条回答
  • 2020-12-04 08:09

    this should explain it.

    for{
        x := 1
        switch {
        case x >0:
            fmt.Println("sjus")
        case x == 1:
            fmt.Println("GFVjk")
        default:
            fmt.Println("daslkjh")
        }
    }
    }
    

    Runs forever

    for{
        x := 1
        switch {
        case x >0:
            fmt.Println("sjus")
            break
        case x == 1:
            fmt.Println("GFVjk")
        default:
            fmt.Println("daslkjh")
        }
    }
    }
    

    Again, runs forever

    BUT

    package main
    
    import "fmt"
    
    func main() {
    d:
    for{
    x := 1
        switch {
        case x >0:
            fmt.Println("sjus")
            break d
        case x == 1:
            fmt.Println("GFVjk")
        default:
            fmt.Println("daslkjh")
        }
    }
    }
    

    will print sjus ... clear ?

    http://play.golang.org/p/GOvnfI67ih

    0 讨论(0)
  • 2020-12-04 08:10

    Just from a switch block. There's plenty of examples in Golang own code you can examine (compare inner break with outer break).

    0 讨论(0)
  • 2020-12-04 08:12

    It only exits the switch block.

    0 讨论(0)
  • 2020-12-04 08:15

    A hopefully illustrative example:

    loop:
    for {
            switch expr {
            case foo:
                    if condA {
                            doA()
                            break // like 'goto A'
                    }
    
                    if condB {
                            doB()
                            break loop // like 'goto B'                        
                    }
    
                    doC()
            case bar:
                    // ...
            }
    A:
            doX()
            // ...
    }
    
    B:
    doY()
    // ....
    
    0 讨论(0)
  • 2020-12-04 08:15

    This question might be too old already but I still think label makes our code become harder to read. Instead of breaking the for inside select, just set a flag for the loop and handle it inside select-case before invoking break. For example:

    loop := true
    for loop {
        select {
        case <-msg:
            // do your task here
        case <-ctx.Done():
            loop = false
            break
        }
    }
    
    0 讨论(0)
  • 2020-12-04 08:17

    Break statements, The Go Programming Language Specification.

    A "break" statement terminates execution of the innermost "for", "switch" or "select" statement.

    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
          }
      }
    

    Therefore, the break statement in your example terminates the switch statement, the "innermost" statement.

    0 讨论(0)
提交回复
热议问题