Skip to main content

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.

The break statement is a control flow construct used to immediately terminate the execution of the innermost enclosing for, switch, or select statement. Upon execution, control is transferred to the statement immediately following the terminated block.

Syntax

Go supports two forms of the break statement: unlabeled and labeled.
// Unlabeled break
break

// Labeled break
break LabelName

Unlabeled Break Mechanics

When executed without a label, break operates strictly on the innermost control structure containing it.
  • In for statements: It halts the loop entirely. Any remaining iterations, including the post-statement (e.g., i++) and the condition evaluation, are bypassed.
  • In switch and select statements: It terminates the execution of the current case block and exits the switch or select structure. Because Go implicitly breaks at the end of each case, an explicit break is mechanically used to short-circuit the execution of a case block before reaching its lexical end.
Execution Flow Visualization:
for i := 0; i < 5; i++ {
    if i == 2 {
        break // Execution jumps directly to Statement A
    }
    // Skipped when i == 2
}
// Statement A: Control resumes here

Labeled Break Mechanics

A labeled break is used to terminate an outer enclosing for, switch, or select statement from within a nested structure. The target label must be declared immediately preceding the control structure it identifies. According to the Go specification, the scope of a label is the entire body of the function in which it is declared (excluding nested functions). Consequently, a label name must be unique across the entire function body. However, labels reside in a separate namespace, meaning they do not conflict with standard identifiers like variables or types in the lexical block scope. Label resolution and control flow jumps are handled by the Go compiler at compile-time, translating directly into jump instructions rather than being dynamically evaluated by the Go runtime. When break LabelName is executed, it terminates the specific control structure associated with LabelName, transferring control to the statement immediately following that labeled block. Execution Flow Visualization:
OuterControl:
    for i := 0; i < 3; i++ {
        switch i {
        case 1:
            for j := 0; j < 3; j++ {
                break OuterControl // Execution jumps directly to Statement B
            }
        }
    }
// Statement B: Control resumes here

Lexical Constraints

  1. Valid Targets: A break statement (labeled or unlabeled) is syntactically invalid if it is not lexically enclosed by a for, switch, or select statement within the same function.
  2. Label Association: A labeled break must refer to a label that directly precedes a for, switch, or select statement. It cannot target arbitrary block labels or labels attached to unsupported statement types (such as if statements).
Master Go with Deep Grasping Methodology!Learn More