标签:ase pack main func ack 使用 wait default efault
package main import ( "fmt" "sync" "time" ) // 为什么需要context? var wg sync.WaitGroup var notify bool func f() { defer wg.Done() for { fmt.Println("==") time.Sleep(time.Millisecond * 500) if notify { break } } } func main() { wg.Add(1) go f() time.Sleep(time.Second * 5) // 如何通知子goroutine退出 notify = true wg.Wait() }
package main import ( "fmt" "sync" "time" ) // 为什么需要context? var wg sync.WaitGroup var exitChan = make(chan bool, 1) func f() { defer wg.Done() FORLOOP: for { fmt.Println("周琳") time.Sleep(time.Millisecond * 500) select { case <-exitChan: break FORLOOP default: } } } func main() { wg.Add(1) go f() time.Sleep(time.Second * 5) // 如何通知子goroutine退出 exitChan <- true wg.Wait() }
标签:ase pack main func ack 使用 wait default efault
原文地址:https://www.cnblogs.com/yzg-14/p/12298514.html