Switch with no condition

Switch without a condition is the same as switch true.

This construct can be a clean way to write long if-then-else chains.

switch-with-no-condition.go

  1. package main
  2. import (
  3. "fmt"
  4. "time"
  5. )
  6. func main() {
  7. t := time.Now()
  8. switch {
  9. case t.Hour() < 12:
  10. fmt.Println("Good morning!")
  11. case t.Hour() < 17:
  12. fmt.Println("Good afternoon.")
  13. default:
  14. fmt.Println("Good evening.")
  15. }
  16. }