xxxxxxxxxx
var value interface{} = "Hello Grepper"
switch vale.(type){
case string:
fmt.Println("It is a string")
case int:
fmt.Println("It is a int")
default:
fmt.Println("Not sure")
}
xxxxxxxxxx
var x interface{} = "foo"
switch v := x.(type) {
case nil:
fmt.Println("x is nil") // here v has type interface{}
case int:
fmt.Println("x is", v) // here v has type int
case bool, string:
fmt.Println("x is bool or string") // here v has type interface{}
default:
fmt.Println("type unknown") // here v has type interface{}
}
xxxxxxxxxx
package main
my_number := 2
fmt.Print("Write ", my_number, " as ")
switch my_number {
case 1:
fmt.Println("one")
case 2:
fmt.Println("two")
case 3:
fmt.Println("three")
default :
// default case
}
xxxxxxxxxx
func main() {
i := 2
switch i {
case 1:
fmt.Println("one")
case 2:
fmt.Println("two")
case 3:
fmt.Println("three")
}
}
xxxxxxxxxx
i := 2
switch i {
case 1:
fmt.Println("one")
case 2:
fmt.Println("two")
default:
fmt.Println("none")
}
xxxxxxxxxx
switch day {
case "sunday":
// cases don't "fall through" by default!
fallthrough
case "saturday":
rest()
default:
work()
}
xxxxxxxxxx
switch t := i.(type) {
case bool:
fmt.Println("I'm a bool")
case int:
fmt.Println("I'm an int")
default:
fmt.Printf("Don't know type %T\n", t)
}