Fragment Description:



A quite fun example of 'switch' case...
to check 'positive or not'? by D.Cheney


switchCaseFunny

Go Playground

Last update, on 2015, Fri 9 Oct, 16:15:35

/* ... <== see fragment description ... */

package main

import "fmt"

// Positive returns true if the number is positive, false if it is negative.
// The second return value indicates if the result is valid, which in the
// case
// of n == 0, is not valid.
func Positive(n int) (bool, bool) {
    if n == 0 {
        return false, false
    }
    return n > -1, true
}
func Check(n int) {
    pos, ok := Positive(n)
    switch {
    case pos == true && ok == true:
        fmt.Println(n, "is positive")
    case pos == false && ok == true:
        fmt.Println(n, "is negative")
    default:
        fmt.Println(n, "is neither")
    }
}
func main() {
    Check(1)
    Check(0)
    Check(-1)
}

/* Expected Output:
1 is positive
0 is neither
-1 is negative
*/



Comments