[Go] go中的goto语句跳到指定标签

前端之家收集整理的这篇文章主要介绍了[Go] go中的goto语句跳到指定标签前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

比如下面的语句goto TOP,其中TOP就是自己的自定义标签,下面的TOP:就是要执行的代码
一般用在需要两层循环的地方,里面goto再跳回上面去

    for i := 0; i < 10; i++ {
        if i == 5 {
            goto TOP
        }
    }
TOP:
    fmt.Println("TOP标签里执行的语句")
package main

import strconv"

import fmttime

func main() {
    //测试字符串拼接和字符串与整型的互换
    var str string
    str = tao" + shihan"
    fmt.Println(str)
    var num int
    num,err := strconv.Atoi(1)
    .Println(num,err)

    str = strconv.Itoa(num)
    .Println(str)
TOP:
    测试goto语句
    4 {
            time.Sleep(time.Second)
            goto TOP
        }

    }

}

 

猜你在找的Go相关文章