Everyone is saying write/read golang map concurrently is not safe, but can we just update it but not write new keys?
We know code below would cause fatal error: concurrent map read and map write
.
var m = make(map[int]int)
for i := 0; i < 1000; i++ {
go func() {
m[i] = i
}()
}
Now I'm wondering if we can update map concurrently as below.
var m = make(map[int]int)
// init memory structure of map
for i := 0; i < 1000; i++ {
m[i] = 0
}
// just update values for old keys concurrently
for i := 0; i < 1000; i++ {
go func() {
m[i] = i
}()
}
Can we do that?