Why doesn't this Go Lang code loop work forever with the break label?

I am trying to figure out how the label gap works.

I expect the following program to print "In if statement" forever. This is because the statement break herereturns the execution of the code to the beginning of the loop, which then must be executed again and again.

However, this code is executed only once. What am I missing here?

package main

import "fmt"

func main() {
here:
    for {
        fmt.Println("In if statement")
        break here

    }
    fmt.Println("At the bottom")
}

Execution Result:

In if statement
At the bottom

Program exited.

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

+4
source share
2 answers

This is because the break statement here returns code execution to the beginning of the loop

No, this is not what makes the break. This is not ready. It exits the loop labeled. Execution continues after the loop.

, , ( ).

+8

go :

, "", "switch" "select", - , .

break , , . , ...

+7

Source: https://habr.com/ru/post/1545802/


All Articles