mirror of
https://gitee.com/shikong-sk/golang-study
synced 2025-02-25 00:12:15 +08:00
26 lines
316 B
Go
26 lines
316 B
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"sync"
|
||
|
)
|
||
|
|
||
|
var wg = new(sync.WaitGroup)
|
||
|
|
||
|
func hello(i int) {
|
||
|
defer wg.Done()
|
||
|
fmt.Println("hello:", i)
|
||
|
}
|
||
|
|
||
|
func main() {
|
||
|
for i := 0; i < 100; i++ {
|
||
|
// 开启一个单独的 goroutine 任务
|
||
|
wg.Add(1)
|
||
|
go hello(i)
|
||
|
}
|
||
|
|
||
|
fmt.Println("main")
|
||
|
// 等待所有线程结束
|
||
|
wg.Wait()
|
||
|
}
|