Switch

Wikipedia: Switch statement
switch statements express conditionals across many branches.

fmt := import("fmt")
times := import("times")
i := 2
fmt.print("Write ", i, " as ")
switch i {
case 1:
    fmt.println("one")
case 2:
    fmt.println("two")
case 3:
    fmt.println("three")
}
t := times.now()
switch times.time_weekday(t) {
case 0, 6:
    fmt.println("It's the weekend")
default:
    fmt.println("It's a weekday")
}
hour := times.time_hour(t)
switch {
case hour < 12:
    fmt.println("It's before noon")
default:
    fmt.println("It's after noon")
}
whatAmI := func(v) {
    switch type_name(v) {
    case "bool":
        fmt.println("bool")
    case "int":
        fmt.println("int")
    default:
        fmt.println("other: ", type_name(v))
    }
}
whatAmI(true)
whatAmI(1)
whatAmI("hey")

try it

Write 2 as two
It's the weekend
It's after noon
bool
int
other: string
loading…