代码点读机 / Go / goto

goto Go · 关键字

无条件跳转到同一函数内指定的标签位置继续执行。

怎么用

goto 标签名(标签名后用冒号)

小例子

package main
import "fmt"

func main() {
    i := 0
Loop:
    fmt.Print(i)
    i++
    if i < 3 {
        goto Loop
    }
}

运行结果

输出 012

提醒:goto 不能跳过变量声明,也不能跳入其它函数;现代 Go 代码中极少使用

官方文档:https://go.dev/ref/spec#Goto_statements

在代码里遇到不认识的词?打开代码点读机,点一下就懂 → ← Go全部词条