package main import ( "fmt" "sync" "time" ) // 调用示例 func main() { var w = sync.WaitGroup{} var ch = make(chan bool) w.Add(2) go func() { time.Sleep(time.Second * 2) fmt.Println("我2秒") w.Done() }() go func() { time.Sleep(time.Second * 6) fmt.Println("我6秒") w.Done() }() go func() { w.Wait() ch <- false }() select { case <-time.After(time.Second * 7): fmt.Println("我超时了") case <-ch: fmt.Println("我结束了") } }