我正在使用带有标签的无限 for 循环。在 for 循环的范围之外,我有一个计划函数作为 go 例程运行。当满足某个条件时,我想从预定函数中中断 for 循环。我怎样才能做到这一点?这就是我正在尝试的,由于范围问题,这显然不起作用。
package main
import (
"fmt"
"time"
"sync"
)
func main() {
count := 0
var wg sync.WaitGroup
wg.Add(1)
t := time.NewTicker(time.Second*1)
go func (){
for {
fmt.Println("I will print every second", count)
count++
if count > 5 {
break myLoop;
wg.Done()
}
<-t.C
}
}()
i := 1
myLoop:
for {
fmt.Println("iteration", i)
i++
}
wg.Wait()
fmt.Println("I will execute at the end")
}
弑天下
Smart猫小萌
相关分类