Switch Statement

Learn how to effectively use switch statements in Go, a versatile control structure for decision making

Go's switch statement is a powerful control structure for multiple possible execution paths. It can evaluate expressions and execute code blocks based on the evaluation result, making it a versatile tool beyond simple conditional checks.

Basic Switch Statement

Here's a basic use of a switch statement:

package main

import (
	"fmt"
)

func main() {
	dayOfWeek := 3

	switch dayOfWeek {
	case 1:
		fmt.Println("Monday")
	case 2:
		fmt.Println("Tuesday")
	case 3:
		fmt.Println("Wednesday")
	case 4:
		fmt.Println("Thursday")
	case 5:
		fmt.Println("Friday")
	case 6:
		fmt.Println("Saturday")
	case 7:
		fmt.Println("Sunday")
	default:
		fmt.Println("Invalid day of week")
	}
}

Switch with Conditions

Go supports conditions within the cases, enhancing the flexibility of switch statements.

package main

import (
	"fmt"
)

func main() {
	score := 85

	switch {
	case score >= 90:
		fmt.Println("Grade: A")
	case score >= 80:
		fmt.Println("Grade: B")
	case score >= 70:
		fmt.Println("Grade: C")
	case score >= 60:
		fmt.Println("Grade: D")
	default:
		fmt.Println("Grade: F")
	}
}

Multiple Expressions in a Case

A switch statement can test multiple expressions in a single case using a comma-separated list.

package main

import (
	"fmt"
)

func main() {
	day := "Saturday"

	switch day {
	case "Saturday", "Sunday":
		fmt.Println("It's the weekend!")
	default:
		fmt.Println("Weekday")
	}
}

Fallthrough Keyword

By default, Go switch statements do not fall through. If fallthrough behavior is needed, it's explicitly specified using the fallthrough keyword.

package main

import (
	"fmt"
)

func main() {
	age := 18

	switch {
	case age >= 18:
		fmt.Println("Adult")
		fallthrough
	case age >= 13:
		fmt.Println("Teenager")
	default:
		fmt.Println("Child")
	}
}

Best Practices

  • Use switch for cleaner and more readable multi-condition checks compared to long if-else if chains.
  • By leveraging implicit breaks, write more concise code without manually breaking out of each case block.
  • Use fallthrough sparingly and only when necessary, as it can lead to unintentional behaviors if not handled carefully.

Common Pitfalls

  • Forgetting that switch without an expression defaults to switch true, which can alter your case logic.
  • Misusing fallthrough can unintentionally cause execution of subsequent case blocks, so use only when then logic truly demands it.
  • Not handling all possible cases, especially default cases, can lead to unpredicted behavior if no other case matches.

Performance Tips

  • Opt for switch over if-else when checking against multiple values; it's both more efficient and clearer.
  • Placing the most likely or frequent cases first can improve execution performance slightly, as evaluation stops on the first matching case.
  • Avoid complex expressions in switch conditions for performance critical code; evaluate the expression before the switch if possible.