An important question can now be addressed; what if we need to share a piece of data between two different goroutines?
In programs that make use of multiple threads, the common approach to share data between different threads is to lock the variables that are shared between the threads. This is typically known as the sharing memory approach. The following diagram demonstrates how two threads will share memory, by sharing a variable called X:
In Go, there is a very popular motto:
What does that mean? It simply means that Go does not typically prefer sharing memory (there are exceptions, however) between threads through the lock approach. Instead, Go prefers to communicate the data from one goroutine to another. This communicate part is achieved through the Go channels. The following...