> For the complete documentation index, see [llms.txt](https://ineelhere.gitbook.io/code-with-go/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ineelhere.gitbook.io/code-with-go/7-switch.md).

# Switch

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

Let's break down the code step by step:

```go
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
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://ineelhere.gitbook.io/code-with-go/7-switch.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
