> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Go Type Switch Statement

A type switch is a control flow construct that performs a series of type assertions on an interface value. Instead of comparing values, it compares the dynamic type of the interface against a set of specified types, executing the block of code associated with the first matching type.

```go theme={"dark"}
package main

import "fmt"

func main() {
    var interfaceValue any = "golang"

    switch interfaceValue.(type) {
    case int:
        // Executes if the dynamic type is int
        fmt.Println("Type is int")
    case string, bool:
        // Executes if the dynamic type is string or bool
        fmt.Println("Type is string or bool")
    case nil:
        // Executes if the interface value has no dynamic type
        fmt.Println("Interface is nil")
    default:
        // Executes if no other cases match
        fmt.Println("Unknown type")
    }
}
```

## Core Mechanics

**The TypeSwitchGuard and `.(type)` Operator**
The syntax `i.(type)` is syntactically similar to a standard type assertion, but the keyword `type` replaces the specific type identifier. This construct is strictly reserved for use within the `TypeSwitchGuard` (the switch expression) of a `switch` block and cannot be evaluated as a standalone expression.

**Optional Initialization Statement**
Like standard expression switches, a type switch can include an optional initialization statement that precedes the `TypeSwitchGuard`. The initialization statement and the guard are separated by a semicolon (e.g., `switch init(); v := i.(type) { ... }`).

**Variable Binding and Type Inference**
When the `TypeSwitchGuard` includes a short variable declaration (e.g., `v := i.(type)`), the compiler binds a new variable `v` within each `case` block. The static type of `v` depends on the structure of the `case`:

* **Single-Type Case:** If the `case` specifies exactly one type (e.g., `case string:` or `case io.Reader:`), the variable `v` is strongly typed as that specific type within the block.
* **Multiple-Type Case:** If the `case` specifies a comma-separated list of types (e.g., `case int, float64:`), the variable `v` retains the original interface type of the evaluated expression.
* **Default Case:** Within the `default` block, the variable `v` retains the original interface type.

**Interface Types in Cases**
A `case` can specify an interface type rather than a concrete type. When an interface type is evaluated, the type switch checks whether the dynamic type of the value *implements* that interface, rather than checking for strict type equality. If the implementation check succeeds, the bound variable `v` takes on the static type of that matched interface.

**The `nil` Case**
A type switch can explicitly match `nil`. This case evaluates to true if the interface value itself has no dynamic type (e.g., an uninitialized interface or an interface explicitly assigned `nil`).

**Fallthrough Restriction**
Unlike standard expression switches in Go, the `fallthrough` statement is strictly prohibited within a type switch. The compiler will reject any attempt to fall through from one type case to the next.

## Structural Example

```go theme={"dark"}
package main

import (
    "fmt"
    "io"
    "strings"
)

func evaluateType(i any) {
    // Optional initialization statement (val := i) followed by the TypeSwitchGuard
    switch val := i; v := val.(type) {
    case int:
        // v is statically typed as 'int' here.
        // Safe to perform integer operations.
        fmt.Printf("Integer: %d\n", v+10)
    case io.Reader:
        // Interface type case: matches if the dynamic type implements io.Reader.
        // v is statically typed as 'io.Reader' here.
        buf := make([]byte, 4)
        _, _ = v.Read(buf)
        fmt.Printf("Reader contains: %s\n", string(buf))
    case string, bool:
        // Multiple types specified.
        // v remains statically typed as 'any' (interface{}).
        fmt.Printf("String or Bool: %v\n", v)
    case nil:
        // val is a nil interface (no dynamic type).
        // v remains statically typed as 'any'.
        fmt.Println("Value is nil")
    default:
        // No types matched.
        // v remains statically typed as 'any'.
        fmt.Printf("Default case: %T\n", v)
    }
}

func main() {
    evaluateType(42)
    evaluateType(strings.NewReader("test"))
    evaluateType(true)
    evaluateType(nil)
    evaluateType(3.14)
}
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Go Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
