/*
* The Go switch statement.
*/
package main
import "fmt"
func main() {
// Read an item
fmt.Printf("==> ");
var val int;
fmt.Scan(&val)
switch { // Switch expression defaults to true
case val % 11 == 0:
fmt.Printf("%d is divisible by 11. Wow!\n", val)
case val % 7 == 0:
fmt.Printf("%d is divisible by 7, but not by 11.\n", val)
case val <= 0:
fmt.Printf("Your number %d is not very positive.\n", val)
case val >= 100 && val <= 1000:
fmt.Printf("The number %d is between 100 and 1000, " +
"inclusive\n", val)
case val & (val - 1) == 0:
fmt.Printf("The number %d is a power of 2.\n", val)
default:
fmt.Printf("%d is boring.\n", val)
}
}
Go switch expressions may be of any type. When omitted, the
expression defaults to true. Go switches take the first
match, and don't require expressions to be unique. Thus,
an expressionless switch can serve as a cleaner version of
a bank of if, else/if tests. It just matches the first one
which is true.