Terminating statements conclusion

April 9, 2024

So we’ve made it through the list of 8 types of terminating statements. We conclude this section with a couple additional comments:

Terminating statements

All other statements are not terminating.

A statement list ends in a terminating statement if the list is not empty and its final non-empty statement is terminating.

What is a statement list? It’s, well, eh… a list of statements!

It’s not as interesting or profound as it sounds. But let’s illustrate with an example:

var name = "Bob"
fmt.Printf("Hello, %s", Bob)

That’s a statement list, containing two statements. Statement lists can also contain a single statement, or even zero statements.

Statement lists are the body of code blocks.

So how does this relate to our discussion of terminating statements? Well, to state the obvious, as we just read, a statement list ends in a terminating statement if… its last statement is terminating. LOL. Thanks for spelling that out.

A code block with, whose statement list obviously ends with a terminating statement:

{
	fmt.Println("Hello, world")
	return
}

I suppose the one mildly interesting point here is that an empty statement list is not terminating:

{}

☝️ Not terminating!

On the other hand, this code:

for {}

is terminating. But notice, it’s the for statement that is terminating, not the (empty) statement list inside the loop!

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


Terminating labeled statements

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


Terminating select statements

Terminating statements … A “select” statement in which: there are no “break” statements referring to the “select” statement, and the statement lists in each case, including the default if present, end in a terminating statement. This is quite similar to the “switch” case we looked at yesterday. Let’s consider an example: select { case <-ctx.Done(): return ctx.Err() case msg := <-messageChannel fmt.Println(message) return nil } This select statement has two cases, each of which terminates (by virtue of returning).


Terminating switch statements

Terminating statements … A “switch” statement in which: there are no “break” statements referring to the “switch” statement, there is a default case, and the statement lists in each case, including the default, end in a terminating statement, or a possibly labeled “fallthrough” statement. This should make good sense, but let’s look at an example, and a counter-example, to illustrate. func greet(args ...string) error { switch len(args) { case 0: return errors.

Get daily content like this in your inbox!

Subscribe