package main import ( "fmt" "sync" ) var wg sync.WaitGroup func hello(i int) { fmt.Printf("hello,欢迎来到搜寻编程教程%v\n", i) defer wg.Done()//goroutine结束计数器-1 } func main() { for i := 0; i < 10; i++ { go hello(i) wg.Add(1)//启动一个goroutine计数器+1 } wg.Wait()//等待所有的goroutine执行结束 }