Break 与 标签情况
Break 语句
- Go 语言中的 break 语句可以用于,立即跳出 switch、for 和 select
- 但不同的是 Go 语言中的 break 语句可以指定标签
package main
import (
"fmt"
)
func main() {
for i := 0; i < 3; i++ {
for j := 0; j < 10; j++ {
if j == 2 {
break
}
fmt.Println("oter j =", j)
}
}
fmt.Println("come here oter!")
fmt.Println(" ")
}
[Running] go run "/Users/wangyang/go/src/test/main.go"
oter j = 0
oter j = 1
oter j = 0
oter j = 1
oter j = 0
oter j = 1
come here oter!
[Done] exited with code=0 in 0.208 seconds
个人解释
break 达到条件后退出了当前的循环体 for j := 0; j < 10; j++ {,所以多次外次循环
Break 与 标签跳转
- 标签必须在使用之前定义
- 标签后面只能跟 switch 和循环语句,不能插入其它语句
- 跳转到标签之后 switch 和循环不会再次被执行
package main
import (
"fmt"
)
func main() {
oter:
for i := 0; i < 3; i++ {
for j := 0; j < 10; j++ {
if j == 2 {
break oter
}
fmt.Println("oter j =", j)
}
}
}
[Running] go run "/Users/wangyang/go/src/test/main.go"
oter j = 0
oter j = 1
[Done] exited with code=0 in 0.209 seconds
package main
import (
"fmt"
)
func main() {
oter:
switch num := 2; num {
case 1:
fmt.Println("1")
case 2:
fmt.Println("2")
break oter
default:
fmt.Println("other")
}
fmt.Println("come here")
}
[Running] go run "/Users/wangyang/go/src/test/main.go"
2
come here
[Done] exited with code=0 in 0.207 seconds
Continue 与 标签情况
continue 语言
- Go 语言中的 continue 语句可以用于立即进入下一次循环
- 但不同的是 Go 语言中的 continue 语句可以指定标签
package main
import (
"fmt"
)
func main() {
for i := 0; i < 3; i++ {
fmt.Println("i =========================", i)
for j := 0; j < 4; j++ {
if j == 2 {
continue
}
fmt.Println("j=", j)
}
}
fmt.Println("come here")
}
[Running] go run "/Users/wangyang/go/src/test/main.go"
i ========================= 0
j= 0
j= 1
j= 3
i ========================= 1
j= 0
j= 1
j= 3
i ========================= 2
j= 0
j= 1
j= 3
come here
[Done] exited with code=0 in 0.213 seconds
个人解释
2 未达成所以跳过了此次循环,无打印 2
Continue 与 标签跳转
- 标签必须在使用之前定义
- 标签后面只能跟循环语句,不能插入其它语句
- 对于单层循环和直接编写 continue 一样
- 对于多层循环,相当于跳出最外层循环继续判断条件执行
package main
import (
"fmt"
)
func main() {
oter:
for i := 0; i < 3; i++ {
fmt.Println("i=================", i)
for j := 0; j < 4; j++ {
if j == 2 {
continue oter
}
fmt.Println("j=", j)
}
}
fmt.Println("come here")
}
[Running] go run "/Users/wangyang/go/src/test/main.go"
i================= 0
j= 0
j= 1
i================= 1
j= 0
j= 1
i================= 2
j= 0
j= 1
come here
[Done] exited with code=0 in 0.208 seconds
goto 语句
Go 语言中的 goto 用于在同一个函数中瞎跳
package main
import "fmt"
func main() {
num := 1
outer:
if num <= 10 {
fmt.Println(num)
num++
goto outer
}
fmt.Println("come here")
}
[Running] go run "/Users/wangyang/go/src/test/main.go"
1
2
3
4
5
6
7
8
9
10
come here
[Done] exited with code=0 in 0.204 seconds
package main
import "fmt"
func main() {
num := 1
if num <= 10 {
fmt.Println(num)
num++
goto outer
}
outer:
fmt.Println("come here")
}
[Running] go run "/Users/wangyang/go/src/test/main.go"
1
come here
[Done] exited with code=0 in 0.21 seconds
return
Golang 语言中 return 语句和 C 语言一模一样,都是用于结束函数,将结果返回给调用者
package main
import "fmt"
func main() {
fmt.Println(test())
}
func test() (a int) {
a = 1
fmt.Println("can i see ?")
return
}
[Running] go run "/Users/wangyang/go/src/test/tempCodeRunnerFile.go"
can i see ?
1
[Done] exited with code=0 in 0.245 seconds
评论区