Switch evaluation order

Switch cases evaluate cases from top to bottom, stopping when a case succeeds.

(For example,

  1. switch i {
  2. case 0:
  3. case f():
  4. }

does not call f if i==0.)

Note: Time in the Go playground always appears to start at 2009-11-10 23:00:00 UTC, a value whose significance is left as an exercise for the reader.

switch-evaluation-order.go

  1. package main
  2. import (
  3. "fmt"
  4. "time"
  5. )
  6. func main() {
  7. fmt.Println("When's Saturday?")
  8. today := time.Now().Weekday()
  9. switch time.Saturday {
  10. case today + 0:
  11. fmt.Println("Today.")
  12. case today + 1:
  13. fmt.Println("Tomorrow.")
  14. case today + 2:
  15. fmt.Println("In two days.")
  16. default:
  17. fmt.Println("Too far away.")
  18. }
  19. }