> ## 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 Labeled Statement

A labeled statement in Go is an identifier followed by a colon (`:`) that precedes a statement, serving as an explicit execution target for `goto`, `break`, and `continue` control flow operations.

## Syntax

```go theme={"dark"}
LabelName: Statement
```

A label can also be declared on its own line, implicitly attaching to the immediately following statement or acting as an empty statement target at the end of a block.

```go theme={"dark"}
LabelName:
    for {
        // ...
    }
```

## Scope and Namespace

* **Function Scope:** Labels are scoped to the function in which they are declared. They are not block-scoped. A label declared anywhere in a function is visible throughout the entire function, but cannot be referenced from nested functions, closures, or outside the function.
* **Independent Namespace:** Labels exist in a separate namespace from variables, types, and functions. A label can share an identifier with a variable without causing a shadowing conflict or compilation error.
* **Strict Utilization:** Go enforces strict label utilization. Declaring a label without subsequently referencing it with a `goto`, `break`, or `continue` statement results in a compile-time error (`label X defined and not used`).

## Control Flow Mechanics

Labels modify the default behavior of three specific control flow statements:

### 1. `break`

When `break` is followed by a label, it terminates the execution of the specific `for`, `switch`, or `select` statement associated with that label, rather than just the innermost enclosing block.

```go theme={"dark"}
Outer:
    for i := 0; i < 10; i++ {
        switch i {
        case 5:
            break Outer // Terminates the 'for' loop, not just the 'switch'
        }
    }
```

### 2. `continue`

When `continue` is followed by a label, it skips the remaining execution of the current iteration and advances to the next iteration of the specific `for` loop associated with that label. The label *must* be attached directly to a `for` statement.

```go theme={"dark"}
LoopTarget:
    for i := 0; i < 3; i++ {
        for j := 0; j < 3; j++ {
            if j == 1 {
                continue LoopTarget // Advances 'i', skipping the rest of the inner loop
            }
        }
    }
```

### 3. `goto`

The `goto` statement transfers control unconditionally to the corresponding labeled statement within the same function.

Go imposes strict lexical restrictions on `goto` jumps to prevent invalid program states:

* A `goto` statement must not cause any variables to come into scope that were not already in scope at the point of the `goto`.
* A `goto` cannot jump into a nested block from outside that block.

```go theme={"dark"}
func gotoMechanics() {
    goto ValidTarget // Allowed: no new variables come into scope between here and the target

ValidTarget:
    // Execution resumes here
    
    // goto InvalidTarget // Compile error: causes 'x' to come into scope

    x := 10
    _ = x

// InvalidTarget:
    
    // goto NestedTarget // Compile error: cannot jump into a nested block
    
    {
// NestedTarget:
    }
}
```

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