package main
import (
"fmt"
"sync"
"time"
)
func main() {
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
fmt.Println("First goroutine sleep")
time.Sleep(1 * time.Second)
}()
wg.Add(1)
go func() {
defer wg.Done()
fmt.Println("Second goroutine sleep")
time.Sleep(2 * time.Second)
}()
wg.Wait()
fmt.Println("All goroutines complete")
}
程序输出如下,
有疑问加站长微信联系(非本文作者)