Is there a when expression in GO

In Kotlin there is a when expression, that worked same like switch/case, but in different format, is this something like this in GO?

when (x) {
    1 -> print("x == 1")
    2 -> print("x == 2")
    else -> { // Note the block
        print("x is neither 1 nor 2")
    }
}

I don’t see any difference to switch/case…

PS: all conditionals in go are statements.

1 Like

May I selected bad example, how below is done neatly with when but not that smoothness with case:

when (x) {
    in 1..10 -> print("x is in the range")
    in validNumbers -> print("x is valid")
    !in 10..20 -> print("x is outside the range")
    60, 90, 120 -> print("x is in my favorite list") 
    else -> print("none of the above")
}

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.