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.
In other words, you can’t break
to a label that labels a more deeply nested for
, switch
or select
statement, or to one that’s completely unrelated.
But that’s only intuitive, right?
OuterLoop: for i = 0; i < n; i++ { for j = 0; j < m; j++ { switch a[i][j] { case nil: state = Error break OuterLoop case item: state = Found break OuterLoop } } }
Speaking of labels, did you know that you can have blank labels? But that you can’t jump to them? Which, of course, raises the question: why have them at all!
_: // A blank label. Can't do anything with it. Other than look at it, I guess
I did a video about some esoteric blank label bugs in Go and in golangci-lint, if you’re interested in random silly trivia.
Quotes from The Go Programming Language Specification Language version go1.22 (Feb 6, 2024)