Go编程语言中的 goto 语句提供了从goto到同一函数中带标签的语句的无条件跳转。
Go中 goto 语句的语法如下-
goto label; .. . label: statement;
在这里, label 可以是Go关键字以外的任何纯文本,并且可以在Go程序中位于 goto 语句上方或下方的任何位置进行设置。
package main import "fmt" func main() { /* 局部变量定义 */ var a int=10 /* do循环执行 */ LOOP: for a < 20 { if a == 15 { /* 跳过迭代 */ a=a + 1 goto LOOP } fmt.Printf("value of a: %d\n", a) a++ } }
编译并执行上述代码后,将产生以下输出-
链接:https://www.learnfk.comhttps://www.learnfk.com/go/go-goto-statement.html
来源:LearnFk无涯教程网
value of a: 10 value of a: 11 value of a: 12 value of a: 13 value of a: 14 value of a: 16 value of a: 17 value of a: 18 value of a: 19
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)