break

break

swtich到底跳到哪

只能跳出自己的swtich

package main

import (
	"fmt"
	"time"
)

func main() {
	t := 0

	for {
		switch t {
		case 3:
			break
		default:
			t++
			fmt.Println(t)
		}

		fmt.Println("over1")
		time.Sleep(time.Second*2)
	}

	fmt.Println("over2")
}

## 输出
over1
2
over1
3
over1
over1
over1
over1
一直输出over1

跳到指定位置

package main

import (
	"fmt"
	"time"
)

func main() {
	t := 0

LOOP:
	for {
		for {
			switch t {
			case 3:
				break LOOP
			default:
				t++
				fmt.Println(t)
			}

			fmt.Println("over1")
			time.Sleep(time.Second * 2)
		}
	}

	fmt.Println("over2")
}


## 输出
1
over1
2
over1
3
over1
over2
posted @ 2021-01-10 19:45  maob  阅读(88)  评论(0编辑  收藏  举报