在Go编程语言中的goto语句提供无条件跳转从跳转到标记声明的功能。
注意:使用goto语句是高度劝阻的在任何编程语言,因为它使得难以跟踪程序的控制流程,使程序难以理解,难以修改。使用一个goto任何程序可以改写,以便它不需要goto。
转到goto语句的语法如下:
goto label;
..
.
label: statement;
在这里,标签(label)可以是除去关键字任何纯文本,它可以在任何地方设置在Go程序的上方或下方,以使用goto语句。
package main
import "fmt"
func main() {
/* local variable definition */
var a int = 10
/* do loop execution */
LOOP: for a < 20 {
if a == 15 {
/* skip the iteration */
a = a + 1
goto LOOP
}
fmt.Printf("value of a: %d\n", a)
a++
}
}
让我们编译和运行上面的程序,这将产生以下结果:
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