package main import ( "fmt" "time" ) func main() { // The switch statement is like a multi-way if. It runs the first case whose value is equal to the condition. i := 2 fmt.Print("Write ", i, " as ") switch i { case 1: fmt.Println("one") //no fallthrough, so the next case will not be executed //no break statement needed, unlike in C or Java case 2: fmt.Println("two") case 3: fmt.Println("three") } switch time.Now().Weekday() { // You can use commas to separate multiple expressions in the same case statement. case time.Saturday, time.Sunday: fmt.Println("It's the weekend") default: fmt.Println("It's a weekday") } t := time.Now() switch { case t.Hour() < 12: // Switch without an expression is an alternate way to express if/else logic. // Here we use it to show how the current hour falls into the first or second half of the day. fmt.Println("It's before noon") default: fmt.Println("It's after noon") } // In Go, a type switch is a construct that permits several type assertions in series. whatAmI := func(i interface{}) { 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) } } whatAmI(true) whatAmI(1) whatAmI("hey") }