In Go, I can use a switch
without a condition, and instead provide the conditions in the case
branches, such as:
package main
import (
"fmt"
"time"
)
func main() {
t := time.Now()
switch {
case t.Hour() < 12:
fmt.Println("Good morning!")
case t.Hour() < 17:
fmt.Println("Good afternoon.")
default:
fmt.Println("Good evening.")
}
}
(Taken from https://tour.golang.org/flowcontrol/11)
What I like about this approach is that it is much cleaner than if-else if-else if-…
. Unfortunately, this construct is not possible in JavaScript.
How could I create something that looks like this as closely as possible, using some (weird) language constructs?