Go maps are only concurrent safe if all accesses are reads (lookups) after
the map is initialized. So your scenario is not safe and can result in a
panic as well as other undefined behavior. The Go standard library has a
concurrent safe map implementation (see https://pkg.go.dev/sync#Map). There
are also many third-party implementations that may be better suited for
your use case. Alternatively, you can, of course, use a mutex to serialize
access but it's probably safer to just use a concurrent safe implementation.

On Tue, Aug 2, 2022 at 6:31 PM ag9920 <jingong.w...@gmail.com> wrote:

> Hi! If I have several 5 goroutines read/write 5 different keys in map
> independently without a Mutex/RWMutex. Each goroutine just read/write their
> own corresponding key. No intersection.
>
> In such a case, no goroutines will operate on the same key, does that mean
> it's safe?
>
> Or maybe each goroutine just write to its corresponding key once, with no
> read, will that be safe?
>
> --
> 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.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/golang-nuts/76788485-6809-4397-bf94-536a0d0bf0b3n%40googlegroups.com
> <https://groups.google.com/d/msgid/golang-nuts/76788485-6809-4397-bf94-536a0d0bf0b3n%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>


-- 
Kurtis Rader
Caretaker of the exceptional canines Junior and Hank

-- 
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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CABx2%3DD8ZsUO71KjB4n4o8MZzraO%2B5R-RHoHGMJpqyykcaxY7gg%40mail.gmail.com.

Reply via email to