Terminating statements

March 27, 2024

Terminating statements

A terminating statement interrupts the regular flow of control in a block. The following statements are terminating:

  1. A “return” or “goto” statement.

The return statement should be familiar to virtually everyone. And the fact that it interrupts the regular flow of control should be no surprise:

func foo() {
	fmt.Println("Hello there")
	return
	fmt.Println("You'll never see me!")
}

In this above example, the second fmt.Println is never called for fairly obvious reasons: The preceeding return statement interrupts the flow, and returns control control flow back to whatever code called foo() in the first place.

Less well known is Go’s goto statement (which we’ll cover in detail later). You’ll rarely, if ever, use this. I’ve used it maybe once or twice. I’ve seen it a few times. It’s mostly useful deep in the bowels of highly-optimized code, where most of us rarely venture. Although I’m sure a few of you are shaking your heads as you read this thinking that you use it all the time. You’re probably one of those develoeprs who works on highly-optimized coded a lot! Thanks for doing it, so I don’t have to. LOL

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 .

Get daily content like this in your inbox!

Subscribe