Terminating switch statements

April 4, 2024

Terminating statements

  1. 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.New("not enough arguments")
		case 1:
			fmt.Println("Hello", os.Args[1])
			return nil
		default:
			return errors.New("too many arguments")
	}
	// <--- Notice, no return statement
}

In the above example, the switch statement meets all the requirements: No break statement, there is a default case, and each case ends in a terminating statement (a return). AS a result, the switch statement itself is a terminating statement, so no additional terminating statement is needed after.

A common alternative to this pattern is to omit the default case, in which case the switch is no longer terminating:

func greet(args ...string) error {
	switch len(args) {
		case 0:
			return errors.New("not enough arguments")
		case 1:
			fmt.Println("Hello", os.Args[1])
			return nil
	}
	return errors.New("too many arguments")
}

In this case, the code is functionally equivalent, however. We’ve just moved the default case out of the switch statement.

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


Fallthrough statements

Fallthrough statements A “fallthrough” statement transfers control to the first statement of the next case clause in an expression “switch” statement. It may be used only as the final non-empty statement in such a clause. FallthroughStmt = "fallthrough" . Well that’s pretty straight forward. for x := range 10 { switch { case x%2 == 0: fmt.Print("X") fallthrough case x%3 == 0: fmt.Print("x") fallthrough case x%5 == 0: fmt.Print("O") } fmt.


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

Get daily content like this in your inbox!

Subscribe