官术网_书友最值得收藏!

Go channels

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 approachThe following diagram demonstrates how two threads will share memory, by sharing a variable called X:

In Go, there is a very popular motto:

"Do not communicate by sharing memory; instead, share memory by communicating."

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 diagram demonstrates how this looks visually:

Let's take a look at regular and buffered channels in the next sections.

主站蜘蛛池模板: 诸城市| 临颍县| 汶上县| 延庆县| 阿拉尔市| 察隅县| 腾冲县| 从江县| 金门县| 卫辉市| 永平县| 凤冈县| 曲松县| 宝丰县| 湘潭县| 大名县| 秭归县| 乌兰察布市| 天气| 东辽县| 德州市| 苍溪县| 开阳县| 丰顺县| 三门县| 海南省| 游戏| 隆安县| 孝义市| 隆德县| 南丰县| 大港区| 丹江口市| 四子王旗| 霸州市| 曲水县| 磴口县| 诸城市| 青铜峡市| 麻阳| 陆川县|