下面的代码每次执行后都会打印不同的值,但我希望这些值相同,如何在不使用的情况下更改下面的代码time.Sleep
package main
import (
"fmt"
"sync"
)
var total int
var wg sync.WaitGroup
// Inc increments the counter for the given key.
func inc(num int) {
total += num
wg.Done()
}
// Value returns the current value of the counter for the given key.
func getValue() int {
return total
}
func main() {
for i := 1; i <= 1000; i++ {
wg.Add(1)
go inc(i)
}
wg.Wait()
fmt.Println(getValue())
}
 慕桂英546537
慕桂英546537 
					幕布斯7119047
 
					慕虎7371278
 
					慕桂英3389331
 随时随地看视频慕课网APP
随时随地看视频慕课网APP
相关分类