Go 编程语言中的 goto 语句提供从 goto 到同一函数中 labeled 语句的无条件跳转。
注意 − 在任何编程语言中都强烈建议使用 goto 语句,因为很难跟踪程序的控制流,从而使程序难以理解和修改。任何使用 goto 的程序都可以使用其他构造重写。
语法
Go 中 goto 语句的语法如下 -
在这里,label 可以是除 Go 关键字之外的任何纯文本,并且可以在 Go 程序中任意位置设置为 goto 语句的上方或下方。
流程图
例
编译并执行上述代码时,它会产生以下结果——
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
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