I wonder what is the most down to earth explanation of this famous quote:
Don\'t communicate by sharing memory; share memory by communicating. (R. Pik
There are two sentences in this; for fuller understanding these must be looked separately first, and then clubbed together, So:
Don't communicate by sharing memory;
means, different threads should not communicate with each other by complying to the stringent and error-prone memory visibility and synchronization policies like memory barrier etc. (Note that it can be done, but it can soon become complex and very buggy with data races). So avoid complying to manual, programmatic visibility constructs mostly attained thru proper synchronizations in programming languages like Java.
share memory by communicating.
means if one thread has made any changes (writes) to a memory area, it should communicate the same (the memory area) to the thread interested in that same memory area; note this has already restricted the scope of memory to only two threads.
Now read the above two paragraphs in conjunction with the golang memory model which says, A send on a channel happens before the corresponding receive from that channel completes.
The happens-before relationship confirms that the write by first goroutine will-be visible to the second goroutine receiving the memory reference at the other end of the channel!