GO foreign Language translation project golang.org/doc/effecti…
Concurrent programming is a big topic, and only a few go-specific highlights have been set aside here.
In many environments, concurrent programming becomes difficult due to the nuances required to achieve correct access to shared variables. Go encourages a different approach, where shared values are passed over channels and, in practice, are never actively shared by individual threads of execution. Only one Goroutine can access this value at any given time. By design, data competition does not occur. To encourage this way of thinking, let’s reduce it to a simple, easy-to-understand summary:
Do not communicate over shared memory; Instead, memory should be shared through communication.
This approach can go too far. For example, the best way is to place a mutex around an integer variable. But as an advanced approach, using channels to control access makes it easier to write clean, correct programs.
One way to think about this model is to consider a typical single-threaded program running on a CPU. It does not require synchronization primitives. Now run another such instance; It also doesn’t need to synchronize. Now let the two of them talk; If the communication is a synchronizer, no additional synchronizations are required. For example, Unix pipes are a good fit for this model. Although Go’s concurrent approach stems from Hoare’s Communication Sequence process (CSP), it can also be seen as a type-safe generalization of Unix pipelines.