golang type switch case code example
Example 1: type switch golang
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{}
}
Example 2: go switch case
switch time.Now().Weekday() {
case time.Saturday, time.Sunday:
fmt.Println("It's the weekend")
default:
fmt.Println("It's a weekday")
}