func main() {
done := make(chan bool)
ticker := time.NewTicker(time.Millisecond * 1000)
go func() {
total := 0
for t := range ticker.C {
fmt.Println("Tick at:", t)
total += 1
if total > 10 {
break
}
}
done <- true
}()
<-done
ticker.Stop()
}
func main() {
var wg sync.WaitGroup
wg.Add(1)
ticker := time.NewTicker(time.Millisecond * 1000)
go func() {
total := 0
for t := range ticker.C {
fmt.Println("Tick at:", t)
total += 1
if total > 10 {
break
}
}
wg.Done()
}()
wg.Wait()
ticker.Stop()
}
复制代码
本文来自:博客园
查看原文:golang协程进行同步方法返回搜狐,查看更多