Skip to content

Latest commit

 

History

History
97 lines (81 loc) · 2.78 KB

7-switch.md

File metadata and controls

97 lines (81 loc) · 2.78 KB

Switch

The provided Go code demonstrates the usage of the switch statement in various scenarios.

Let's break down the code step by step:

package main

import (
    "fmt"
    "time"
)

func main() {
    // Example 1: Basic switch statement with an integer
    i := 2
    fmt.Print("Write ", i, " as ")
    switch i {
    case 1:
        fmt.Println("one")
    case 2:
        fmt.Println("two")
    case 3:
        fmt.Println("three")
    }

    // Example 2: Switch statement with time.Weekday
    switch time.Now().Weekday() {
    case time.Saturday, time.Sunday:
        fmt.Println("It's the weekend")
    default:
        fmt.Println("It's a weekday")
    }

    // Example 3: Switch statement without an expression
    // Evaluates the current time and checks if it's before or after noon
    t := time.Now()
    switch {
    case t.Hour() < 12:
        fmt.Println("It's before noon")
    default:
        fmt.Println("It's after noon")
    }

    // Example 4: Switch statement with a type assertion
    // Uses a function literal (anonymous function) to determine the type of the input
    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")
}

Explanation:

  1. Basic Switch with Integer (i):

    • The code initializes an integer i with the value 2.
    • The switch statement checks the value of i and prints the corresponding word for the number.
  2. Switch with time.Weekday:

    • The code uses the time.Now().Weekday() function to get the current day of the week.
    • The switch statement checks if it's Saturday or Sunday and prints a message accordingly.
  3. Switch without Expression (t.Hour()):

    • The code uses the current time (time.Now()) and checks the hour using t.Hour().
    • The switch statement doesn't have a specific expression but evaluates conditions based on the hour.
  4. Switch with Type Assertion (whatAmI function):

    • The code defines a function whatAmI that takes an empty interface (interface{}), allowing it to accept values of any type.
    • Inside the function, a switch statement uses type assertion (i.(type)) to determine the type of the input and prints a message accordingly.

    Example outputs:

    • I'm a bool for true
    • I'm an int for 1
    • Don't know type string for "hey"

This code showcases the flexibility and power of the switch statement in Go, allowing developers to handle different types of conditions and expressions efficiently.

Output

Write 2 as two
It's a weekday
It's before noon
I'm a bool
I'm an int
Don't know type string