I am seeing some "fatal error: concurrent map iteration and map write" on some old code written a while back. I've been trying to diagnose the issue, and I ran into a block of code similar to the below playground link.
It seems wrong to me to iterate on a returned map (which is passed by reference) even if it is returned in a function with a lock.
I was looking for an explanation about why this seems to not be throwing the above error.
I understand that there are RWMutex's and sync Maps now, but this code was written a while back, and I am looking for an explanation of the functionality as opposed to improvements.
https://play.golang.org/p/_RY-QAyGE2W
I would assume that the above code would throw "fatal error: concurrent map iteration and map write", however it seems to be working with no issue.
This illustrates it better: https://play.golang.org/p/RiWTxBoNqwD It won't run in the playground but it crashes pretty much right away if you go run it from the commandline.
The iteration of the map isn't protected by the mutex. Only the act of returning the map reference is protected.
This does almost the same but the entire iteration is protected by the mutex: https://play.golang.org/p/cQNs3x_k-SL or this https://play.golang.org/p/ydjprgvF6aE