Junior — Senior
Concurrent read and write to a map in Go
livecode
Task condition
What happens if you perform read and write operations on a map concurrently from different goroutines? Will errors occur, and are there any restrictions?
var data = map[string]int{"a": 1}
func main() {
go reader()
time.Sleep(1 * time.Second)
go writer()
time.Sleep(1 * time.Second)
}
func reader() {
for {
fmt.Println(data["a"])
}
}
func writer() {
for {
data["a"] = 2
}
}