Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

  1. Home
  2. Go Dictionary
  3. goto / Labels

goto / Labels

In Go, you can use the goto statement to jump execution to a specified label. You can also attach a label to a loop so that you can break out of or continue an outer loop in nested loops all at once.

Syntax

// Jump to the specified label using goto.
goto labelName

// Label definition (append a colon at the end)
labelName:
    // code

// Labeled break (breaks out of the outer loop)
outer:
for i := 0; i < 3; i++ {
    for j := 0; j < 3; j++ {
        if j == 1 {
            break outer // Breaks out of the for loop marked with the outer label.
        }
    }
}

// Labeled continue (continues the outer loop)
outer:
for i := 0; i < 3; i++ {
    for j := 0; j < 3; j++ {
        if j == 1 {
            continue outer // Advances to the next iteration of the for loop marked with the outer label.
        }
    }
}

Syntax List

SyntaxDescription
goto labelUnconditionally jumps to the position of the specified label.
label: (for break)Used to break out of the outermost loop in nested loops all at once.
label: (for continue)Used to advance to the next iteration of the outer loop in nested loops.

Sample Code

package main

import "fmt"

func main() {
    // Use goto to skip code (error handling pattern)
    x := 5
    if x < 0 {
        goto errorHandler
    }
    fmt.Println("x is a positive number:", x)
    goto done // Skips errorHandler.

errorHandler:
    fmt.Println("Error: x is negative")

done:
    fmt.Println("Done")

    fmt.Println()

    // Use labeled break to exit nested loops.
    fmt.Println("Labeled break:")
outer:
    for i := 0; i < 3; i++ {
        for j := 0; j < 3; j++ {
            if i == 1 && j == 1 {
                fmt.Printf("  i=%d, j=%d: breaking out of outer\n", i, j)
                break outer // Breaks out of the outer loop entirely.
            }
            fmt.Printf("  i=%d, j=%d\n", i, j)
        }
    }

    fmt.Println()

    // Use labeled continue to advance to the next iteration of the outer loop.
    fmt.Println("Labeled continue:")
loop:
    for i := 0; i < 3; i++ {
        for j := 0; j < 3; j++ {
            if j == 1 {
                continue loop // Advances to the next i in the outer loop.
            }
            fmt.Printf("  i=%d, j=%d\n", i, j)
        }
    }
}

Notes

In Go, goto can only be used to jump to a label within the same function. Jumping over a variable declaration causes a compile error. In practice, goto is occasionally used in error-handling patterns, but labeled break and continue are generally preferred for better readability.

Overusing goto makes the flow of code difficult to follow. Prefer labeled break for breaking out of nested loops, and early return for error handling.

For the basics of loops, see also for.

If you find any errors or copyright issues, please .