点击个人博客,查看更多文章https://elonjelinek.github.io
判断程序的执行结果,并解释原因
package main
import (
"fmt"
"sync"
)
const N = 20
func main() {
wg := sync.WaitGroup{
}
mu := sync.Mutex{
}
m := make(map[int]int)
wg.Add(N)
for i := 0; i < N; i++ {
go func() {
mu.Lock()
defer wg.Done()
m[i] = i
mu.Unlock()
}()
}
wg.Wait()
fmt.Println(len(m))
fmt.Println(m)
}
运行结果