Home Backend Development Golang What are the jump statements in go language?

What are the jump statements in go language?

Dec 26, 2022 pm 04:33 PM
golang go language Jump

The jump statements include: 1. The break statement is used to exit the loop or exit a switch statement to allow the program to continue executing the code after the loop. The syntax is "break;"; 2. The continue statement is used to exit this time. Loop and start the next loop, the syntax is "continue;"; 3. Combined with the label to jump to the specified label statement, the syntax is "label:"; 4. The goto statement is used to unconditionally transfer to the specified line in the program, Syntax "goto tag;... ...tag: expression;".

What are the jump statements in go language?

The operating environment of this tutorial: Windows 7 system, GO version 1.18, Dell G3 computer.

Jump statements in Go flow control

break and continue statements

and others Like programming languages, the Go language supports breaking out of the loop through the break statement and entering the next loop through the continue statement.

We have already demonstrated the basic usage examples of break in the previous tutorial. The default scope of break is the innermost loop body where the statement is located:

arr := [][]int{{1,2,3},{4,5,6},{7,8,9}}
for i := 0; i < 3; i++ {
    for j := 0; j < 3; j++ {
        num := arr[i][j]
        if j > 1 {
            break
        }
        fmt.Println(num)
    }
}
Copy after login

For example, the break here The meaning is to exit the innermost loop when j > 1, otherwise print the number at the current position.

continue is used to ignore the remaining loop body and directly enter the next loop process:

arr := [][]int{{1,2,3},{4,5,6},{7,8,9}}
for i := 0; i < 3; i++ {
    for j := 0; j < 3; j++ {
        num := arr[i][j]
        if j > 1 {
            break
        } else {
            continue
        }
        fmt.Println(num)
    }
}
Copy after login

If we rewrite the program like this, the above code will not print any value, because the continue statement will Ignore the subsequent code and go directly to the next loop.

tag

The difference between break and continue in Go language and other languages ​​is that it supports combining with tags to jump to the specified tag statement, thereby changing these two The default jump logic of the statement, the label statement is declared through the tag::

arr := [][]int{{1,2,3},{4,5,6},{7,8,9}}
ITERATOR1:
for i := 0; i < 3; i++ {
    for j := 0; j < 3; j++ {
        num := arr[i][j]
        if j > 1 {
            break ITERATOR1
        }
        fmt.Println(num)
    }
}
Copy after login

In this way, the break statement that originally exited the current loop body now jumps to the ITERATOR1 label corresponding position, so the corresponding print result is:

1
2
Copy after login

Because at this time break will jump out of the outer loop directly. If break is changed to continue, the print result is as follows:

1
2
4
5
7
8
Copy after login

goto statement

The goto statement is opposed by most linguists, and everyone is warned not to use it, because it can easily cause confusion in the code logic, which can lead to hard-to-find bugs. However, the Go language still supports the goto keyword. The semantics of the goto statement are very simple, which is to jump to a certain label within this function, such as:

arr := [][]int{{1,2,3},{4,5,6},{7,8,9}}

for i := 0; i < 3; i++ {
    for j := 0; j < 3; j++ {
        num := arr[i][j]
        if j > 1 {
            goto EXIT
        }
        fmt.Println(num)
    }
}   

EXIT:
fmt.Println("Exit.")
Copy after login

When the first time j > 1 conditions, the code will jump to the location specified by the EXIT tag and continue subsequent code execution, so the output of the above code is:

1
2
Exit.
Copy after login

[Related recommendations: Go video tutorialprogramming teaching

The above is the detailed content of What are the jump statements in go language?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is the problem with Queue thread in Go's crawler Colly? What is the problem with Queue thread in Go's crawler Colly? Apr 02, 2025 pm 02:09 PM

Queue threading problem in Go crawler Colly explores the problem of using the Colly crawler library in Go language, developers often encounter problems with threads and request queues. �...

What libraries are used for floating point number operations in Go? What libraries are used for floating point number operations in Go? Apr 02, 2025 pm 02:06 PM

The library used for floating-point number operation in Go language introduces how to ensure the accuracy is...

What should I do if the custom structure labels in GoLand are not displayed? What should I do if the custom structure labels in GoLand are not displayed? Apr 02, 2025 pm 05:09 PM

What should I do if the custom structure labels in GoLand are not displayed? When using GoLand for Go language development, many developers will encounter custom structure tags...

In Go, why does printing strings with Println and string() functions have different effects? In Go, why does printing strings with Println and string() functions have different effects? Apr 02, 2025 pm 02:03 PM

The difference between string printing in Go language: The difference in the effect of using Println and string() functions is in Go...

Which libraries in Go are developed by large companies or provided by well-known open source projects? Which libraries in Go are developed by large companies or provided by well-known open source projects? Apr 02, 2025 pm 04:12 PM

Which libraries in Go are developed by large companies or well-known open source projects? When programming in Go, developers often encounter some common needs, ...

How to solve the user_id type conversion problem when using Redis Stream to implement message queues in Go language? How to solve the user_id type conversion problem when using Redis Stream to implement message queues in Go language? Apr 02, 2025 pm 04:54 PM

The problem of using RedisStream to implement message queues in Go language is using Go language and Redis...

What is the difference between `var` and `type` keyword definition structure in Go language? What is the difference between `var` and `type` keyword definition structure in Go language? Apr 02, 2025 pm 12:57 PM

Two ways to define structures in Go language: the difference between var and type keywords. When defining structures, Go language often sees two different ways of writing: First...

How to solve the problem of Golang generic function type constraints being automatically deleted in VSCode? How to solve the problem of Golang generic function type constraints being automatically deleted in VSCode? Apr 02, 2025 pm 02:15 PM

Automatic deletion of Golang generic function type constraints in VSCode Users may encounter a strange problem when writing Golang code using VSCode. when...

See all articles