> For the complete documentation index, see [llms.txt](https://blog.yushunchen.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://blog.yushunchen.com/golang-notes/basics/special-statements.md).

# Special Statements

## Goto statement

&#x20;In Go, goto statement is used to alter the normal execution of a program and transfer control to a labeled statement in the same program. The label is an identifier, it can be any plain text and can be set anywhere in the Go program above or below to goto statement.

```go
package main

import "fmt"

func main() {

	var age int
election:
	fmt.Println("Enter your age ")
	fmt.Scanln(&age)
	if age <= 21 {
		fmt.Println("You cannot drink!")
		goto election
	} else {
		fmt.Print("You can drink!") // program terminates here
	}

}
```

```bash
Enter your age 
13
You cannot drink!
Enter your age
40
You can drink!
```

## Break Statement

In Go, break statement gives you way to break or terminate the execution of innermost “for”, “switch”, or “select” statement that containing it, and transfers the execution to the next statement following it. It is similar to many other languages.

## Labeled Break Statement

The standard unlabeled break statement is used to terminates the nearest enclosing statement. The labeled break statement uses a label after the break statement.

```go
package main

import "fmt"

func main() {

	var i = 0
	var j = 0

outer:
	for i <= 10 {
		i++
		for j <= 5 {
			if j == 3 {
				break outer
			}
			fmt.Printf("Inside loop %d\n", j)
			j++
		}
	}
	fmt.Println("Out of loop")

}
```

```bash
Inside loop 0
Inside loop 1
Inside loop 2
Out of loop
```

## Continue Statement

The continue statement gives you way to skip over the current iteration of any loop. It is similar to many other languages.

## Labeled Continue Statement

The standard unlabeled continue statement is used to skip the current iteration of nearest enclosing loop. The labeled continue statement uses a label after the continue statement.

```go
package main

import "fmt"

func main() {

	var i = 0
	var j = 0

outerfor:
	for i < 3 {
		j = 0
		i++
		for j < 3 {
			j++
			fmt.Printf("i is %d and j is %d\n", i, j)
			if i == 2 {
				fmt.Println("continue here")
				continue outerfor
			}
		}

	}

}
```

```bash
i is 1 and j is 1
i is 1 and j is 2
i is 1 and j is 3
i is 2 and j is 1
continue here
i is 3 and j is 1
i is 3 and j is 2
i is 3 and j is 3
```

## Defer Statement

A defer statement defers the execution of a function until the surrounding function returns.

```go
package main

import "fmt"

func main() {
	defer fmt.Println("world")

	fmt.Println("hello")
}
```

```bash
hello
world
```

{% hint style="info" %}
Deferred function calls are pushed onto a stack. When a function returns, its deferred calls are executed in last-in-first-out order.
{% endhint %}

```go
package main

import "fmt"

func main() {
	fmt.Println("counting")

	for i := 0; i < 5; i++ {
		defer fmt.Println(i)
	}

	fmt.Println("done")
}

```

```bash
counting
done
4
3
2
1
0
```


---

# 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://blog.yushunchen.com/golang-notes/basics/special-statements.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.
