Golang语言的 goto 语句,可以无条件的转移到程序中的指定行。
goto语句通常与条件语句配合使用,可以实现条件转移,跳出循环体等功能
Go程序中不主张使用 goto 语句,以免造成程序流程的混乱
goto基本语法
goto label
…
label: statement
fmt.Println("hello")fmt.Println("hello1")n := 21if n > 20 {goto here}fmt.Println("hello2")fmt.Println("hello3")fmt.Println("hello4")fmt.Println("hello5")here:fmt.Println("hello6")fmt.Println("hello7")hellohello1hello6hello7
return基本语法
使用在方法或者函数中,表示跳出所在的方法或函数
func main() {// returni := 0for ; i < 10; i++ {if i == 5 {return // 跳出main方法(也就是说终止程序), 不再执行后续代码}fmt.Println("0_0", i)}fmt.Println("i=", i)}0_0 00_0 10_0 20_0 30_0 4
