Does go language have goto?

青灯夜游
Release: 2023-01-11 09:23:28
Original
2740 people have browsed it

Go language has goto statement. The goto statement of Go language can unconditionally transfer to the specified line in the process; the goto statement is usually used in conjunction with conditional statements and can be used to implement conditional transfers, jump out of loops and other functions.

Does go language have goto?

Demo environment for this article: Windows 7 system, Go1.11.2 version, Dell G3 computer.

Go language has goto statement.

Recommended tutorial: "golang"

The use of goto in go language

1. Basic introduction to goto

  • The goto statement of Go language can unconditionally transfer to a specified line in the program.

  • The goto statement is often used with conditional statements. It can be used to implement conditional transfer, jump out of loops and other functions.

  • In Go programming, the use of goto statements is generally not recommended to avoid confusing the program flow and making it difficult to understand and debug the program

Grammar

The syntax format of goto is as follows:

goto label;
..
.
label: statement;
Copy after login

2. Goto flow chart

Does go language have goto?

3. Case

package main

import "fmt"

func main() {
	var a = 10
LOOP:
	fmt.Println("执行了goto")
	for a < 20 {
		if a == 15 {
			a += 1    //a满足条件  15+1
			goto LOOP // 因为上面条件满足了  到这条goto语句就会跳转到LOOP:执行
		}
		fmt.Printf("a的值为:%d\n", a)
		a++
	}
}
Copy after login

For more programming-related knowledge, please visit: Introduction to Programming! !

The above is the detailed content of Does go language have goto?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template