Terminating labeled statements

April 8, 2024

Today’s live stream is starting an hour later than usual. Join me at 16:00, CEST, for some live Go coding using TDD.


Terminating statements

  1. A labeled statement labeling a terminating statement.

Labeled statement’s are not used a lot in Go. Terminating labeled statements even less so. But they do exist, just the same. Here’s an example, using goto, and a labeled terminating statement.

func foo() {
	if someCondition {
		goto end
	}
	fmt.Println("unf")
end:
	return
}

Here end is a label, that labels the statement return. Since the label labels a terminating statement, the labeled statement terminates. Funny how that works.

Can we have a labeled terminating statement that isn’t the target of goto?

Well, labels can be the target of goto, break, or continue. We already saw an example of goto. So what about the others?

A break can only ever be referred to from within for loops or select or switch statements, and adding a break to any of these renders the block that contains it no longer terminating.

So that leaves continue. continue can reference a terminating statement, if the loop it’s in is terminating:

loop:
	for {
		continue loop
	}

Silly example of an infinite loop, but it is terminating.

Here’s a plausibly more realistic example:

  var i int

loop:
	for {
		i++
		if i > 10 {
			return
		}
		continue loop
	}

Obviously using continue in both of these examples is silly, as that would be the default behavior anyway. But I trust you can imagine that there’s some other logic to make the use of continue meaningful.

Quotes from The Go Programming Language Specification Language version go1.22 (Feb 6, 2024)


Share this

Direct to your inbox, daily. I respect your privacy .

Unsure? Browse the archive .

Related Content


Continue statements

Let’s continue… haha See what I did there? Bleh, okay. Continue statements A “continue” statement begins the next iteration of the innermost enclosing “for” loop by advancing control to the end of the loop block. The “for” loop must be within the same function. ContinueStmt = "continue" [ Label ] . There’s not a lot necessary to say here. continue works very much like break. The scoping rules are essentially the same.


Break statements with labels

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?


Break statements

Break statements A “break” statement terminates execution of the innermost “for”, “switch”, or “select” statement within the same function. BreakStmt = "break" [ Label ] . We’ll discuss the Label mentioned tomorrow. Until then, the key word here is innermost. This shouldn’t be very surprising to anyone familiar with other programming languages. But despite its familiarity, I’ve been bitten more than once by forgetting this key detail. It’s perhaps easiet to forget when you have, say, a switch statement within a for loop, for example.

Get daily content like this in your inbox!

Subscribe