代码拉取完成,页面将自动刷新
package main
// To wait for multiple goroutines to finish, we can use a wait group
import (
"fmt"
"sync"
"time"
)
// This is the function we'll run in every goroutine
func worker(id int) {
fmt.Printf("Worker %d starting\n", id)
time.Sleep(time.Second)
fmt.Printf("Worker %d done\n", id)
}
func main() {
// This WaitGroup is used to wait for all the goroutines
// launched here to finish.
var wg sync.WaitGroup
// launch several goroutines and increment the WaitGroup counter
// for each
for i := 1; i <= 5; i++ {
wg.Add(1)
// Wrap the worker call in a closure that makes sure to tell
// the WaitGroup that this worker is done.
// This way the worker itself does not have to be aware of
// the concurrency primitives involved in its execution.
go func() {
// defer statement executed, and places
// wg.Done() on a list to be executed prior to the function returning
defer wg.Done()
worker(i)
}()
}
// Block until the WaitGroup counter goes back to 0
// all the workers notified they're done
wg.Wait()
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。