Terminating statements
…
- A block in which the statement list ends in a terminating statement.
What exactly does this mean?
It means that if you have an (explicit) code block that ends in a terminating statement, the block itself is considered a terminating statement.
Confused?
How about an example:
func foo() {
if somethingInterestingHappened() {
fmt.Println("Wow, that was interesting!")
return
}
fmt.Println("That was boring.")
}
In this example, return
is a terminating statement, as we saw [two days ago](/
archive/2024-03-27-terminating-statements/).
The rule today means that the block { fmt.Println("Wow, that was interesting!"); return; }
is also a terminating statement.
Put another way, terminating statements still terminate, even if they’re wraped in an arbitrary number of other code blocks. return
still terminates the function in this example, despite the extra, completely superflous code blocks:
func foo() {
if somethingInterestingHappened() {
{
fmt.Println("Wow, that was interesting!")
{
return
}
}
}
fmt.Println("That was boring.")
}
Not really that profound, after all.
Quotes from The Go Programming Language Specification Language version go1.22 (Feb 6, 2024)