golang-study/base/goroutine/sync/main.go

26 lines
316 B
Go
Raw Normal View History

2021-11-08 23:58:54 +08:00
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()
}