1
keepeye 2020-07-20 15:16:31 +08:00
可以的
|
2
janxin 2020-07-20 15:19:33 +08:00 1
官方文档这里说的很清楚
Note that calls with a positive delta that occur when the counter is zero must happen before a Wait. Calls with a negative delta, or calls with a positive delta that start when the counter is greater than zero, may happen at any time. Typically this means the calls to Add should execute before the statement creating the goroutine or other event to be waited for. If a WaitGroup is reused to wait for several independent sets of events, new Add calls must happen after all previous Wait calls have returned. See the WaitGroup example. |
3
sunxiansong 2020-07-20 15:20:28 +08:00 1
如果你是希望等待所有 goroutine 完成的话,那么 wg.Add(1)最好放到 go 函数外部, defer wg.Done() 放到函数内部
|
4
Vegetable 2020-07-20 15:22:32 +08:00
defer 没问题,但是 Add 最好放外边,虽然他在第一行,但是未必会比 wait 先执行。
|
5
TypeErrorNone 2020-07-20 15:55:30 +08:00
不可以
|
6
reus 2020-07-20 19:11:56 +08:00
这样用是错的,有可能没 Add,Wait 就执行了,起不到 Wait 的作用
|
7
sxfscool 2020-07-21 08:36:56 +08:00
我觉得不行, wait()可能直接过去,一个也等不到
|
8
hijoker OP 这是 minikube 的源代码, 我看到的时候也是惊呆了
|