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)