当选择操作完成时,这个for循环如何中断?

huangapple go评论69阅读模式
英文:

How does this FOR Loop Break when select is done

问题

以下是翻译好的内容:

以下代码等待'results'通道为空,然后DEFAULT分支跳转到标签DONE。

现在的问题是:为什么这会中断FOR循环?似乎FOR循环会继续跳转到DEFAULT并永远不会结束。

FOR循环被中断的原因是什么。

输出如下:

中断 DONE

结束 For


...
DONE:
for {
	select { // 非阻塞
	case result := <-results:
		fmt.Printf("%s:%d:%s\n", result.filename, result.lino,
			result.line)
	default:
		fmt.Println("中断 DONE")
		break DONE
	}
	fmt.Println("结束 Select")
}
fmt.Println("结束 For")
英文:

The following code waits for the 'results' channel to be empty
and then the DEFAULT branch breaks to Label DONE.

Now the question: Why does this break the FOR LOOP?? It seems
that the FOR loop would continue to skip to DEFAULT and never
end.

WHAT IS BREAKING THE FOR LOOP.

The Output is as follows:

Break DONE

End For


...
DONE:
for {
	select { // Nonblocking
	case result := &lt;-results:
		fmt.Printf(&quot;%s:%d:%s\n&quot;, result.filename, result.lino,
			result.line)
	default:
		fmt.Println(&quot;Break DONE&quot;)
		break DONE
	}
	fmt.Println(&quot;END Select&quot;)
}
fmt.Println(&quot;End For&quot;)

答案1

得分: 3

break DONE和C语言或其他语言中的goto DONE不是相同的。标签标记了break DONE语句所在的for循环,当执行该语句时,它会终止该循环,而不是跳转到DONE标签。标记的break语句在你想要从嵌套循环中跳出或者像你的情况一样从select语句中终止循环时非常有用。如果没有标签,break语句只会终止select语句。

英文:

break DONE is not the same as goto DONE in C or other languages. The label marks the for loop which break DONE statement will terminate when executed. It doesn't jump to DONE label, it terminates the for loop marked with the DONE label. Labeled breaks are very usefull when you want to break from nested loops or, like in your case, terminate loop from inside of select. Without a label break would only terminate select.

答案2

得分: 1

如果存在标签,那么它必须是一个封闭的"for"、"switch"或"select"语句的标签,并且该语句是执行终止的语句。

在这个例子中,标签明确标记了"for"语句的终止。

链接:https://golang.org/ref/spec#Break_statements

英文:

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

https://golang.org/ref/spec#Break_statements

The label in this example explicitly marks the "for" to terminate.

答案3

得分: 0

混淆的原因在于你认为在第一个 case 中会被阻塞。事实并非如此。你会无限循环地选择可用的选项,直到遇到 break。在这种情况下,它会指示在第一次迭代中没有从 results 通道接收到任何内容,因此它会自动进入 default 分支,打印出 Break DONE,然后结束。

英文:

The confusion comes from the idea that you're going to block there on the first case. That's not what happens. You iterate the select options indefinitely taking the one available until you encounter the break. In this case, it would indicate there is nothing to receive from the results channel on your first iteration so it automatically falls into the default cause, prints Break DONE and you're done.

答案4

得分: 0

break关键字用于终止程序的执行。当你调试这段代码时,默认选项将被选择,然后break关键字将执行,停止程序的运行。程序将无法到达标签"完成"(DONE)。

英文:

the break keyword used terminate the execution, when you debug this code, the default option will selected and then break keyword perform which stop the program. Program will not able go at DONE label

huangapple
  • 本文由 发表于 2016年2月17日 08:22:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/35445630.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定