It is not safe to do so and you're understandably asking yourself "but why? my goroutines are working on different already existing entries, so what's going on exactly?". It turns out that a go builtin map is an opaque stucture that is free to completely reorganize itself at any write operation. Thus, it must be regarded as a whole when thinking about data races. The docs are very explicit about this restriction, and the runtime and the race detector do their best at crashing to guide you into enforcing the rule.
Although it would be "conceivable" for a map to reorganize itself on read operations (say, to move recently or frequently accessed entries to optimized buckets), go builtin maps don't do that and it is safe to have multiple concurrent readers, as long as no writer wolf enters. HTH Val On Tuesday, May 16, 2017 at 7:10:56 AM UTC+2, Yan Tang wrote: > > Hi, > > I am aware of golang map is not safe for concurrent access, especially > when there is at least one writer. > > However, I want to know that if the map has been pre-populated, and > different coroutines access (read/write) different key/value pair, is it > safe to do so? There is no deletion or adding keys after the > initialization. > > I think it should be safe (having been doing this in C++ quite frequently) > but just want to double check. Thanks. > > Yan > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.