Share Memory By Communicating - The Go Programming Language https://golang.google.cn/doc/codewalk/sharemem/

One suggestion (made by Rob Pike) for concurrent programming is don't (let computations) communicate by sharing memory, (let them) share memory by communicating (through channels). (We can view each computation as a goroutine in Go programming.)

Communicating by sharing memory and sharing memory by communicating are two programming manners in concurrent programming. When goroutines communicate by sharing memory, we use traditional concurrency synchronization techniques, such as mutex locks, to protect the shared memory to prevent data races. We can use channels to implement sharing memory by communicating.

Go provides a unique concurrency synchronization technique, channel. Channels make goroutines share memory by communicating. We can view a channel as an internal FIFO (first in, first out) queue within a program. Some goroutines send values to the queue (the channel) and some other goroutines receive values from the queue.

Along with transferring values (through channels), the ownership of some values may also be transferred between goroutines. When a goroutine sends a value to a channel, we can view the goroutine releases the ownership of some values. When a goroutine receives a value from a channel, we can view the goroutine acquires the ownerships of some values.

Surely, there may be also not any ownership transferred along with channel communications.

The values (whose ownerships are transferred) are often referenced (but are not required to be referenced) by the transferred value. Please note, here, when we talk about ownership, we mean the ownership from the logic view. Unlike Rust language, Go doesn't ensure value ownership from the syntax level. Go channels can help programmers write data races free code easily, but Go channels can't prevent programmers from writing bad concurrent code from the syntax level.

Although Go also supports traditional concurrency synchronization techniques. only channel is first-class citizen in Go. Channel is one kind of types in Go, so we can use channels without importing any packages. On the other hand, those traditional concurrency synchronization techniques are provided in the sync and sync/atomic standard packages.

Honestly, each concurrency synchronization technique has its own best use scenarios. But channel has a wider application range and has more variety in using. One problem of channels is, the experience of programming with channels is so enjoyable and fun that programmers often even prefer to use channels for the scenarios which channels are not best for.

https://go101.org/article/channel.html

Share Memory By Communicating 一等公民的更多相关文章

  1. Share Memory By Communicating

    Share Memory By Communicating - The Go Programming Language https://golang.google.cn/doc/codewalk/sh ...

  2. scala 学习笔记(07) 一等公民的函数

    在scala中一切皆对象,一切皆函数,函数跟Int,String.Class等其它类型是处于同等的地位,换句话说,使用函数跟使用普通的类型一样,没什么区别,因此: 1.函数可以赋值给变量,可以当参数传 ...

  3. javascript世界一等公民—函数

    简介 在很多传统语言(C/C++/Java/C#等)中,函数都是作为一个二等公民存在,你只能用语言的关键字声明一个函数然后调用它,如果需要把函数作为参数传给另一个函数,或是赋值给一个本地变量,又或是作 ...

  4. Function Programming - First Class(一等公民function)

    引用外界一等公民的定义:"在JavaScript世界中函数却是一等公民,它不仅拥有一切传统函数的使用方式(声明和调用),而且可以做到像简单值一样赋值.传参.返回,这样的函数也称之为第一级函数 ...

  5. JS中的一等公民:函数

    在JavaScript中,函数可以 作为值赋给一个变量 作为参数传递给另一个函数 作为另一个函数的返回值 所以我们说JavaScript的函数是“一等公民”. 赋值: var foo = functi ...

  6. JavaScript世界的一等公民—— 函数

    简介 在很多传统语言(C/C++/Java/C#等)中,函数都是作为一个二等公民存在,你只能用语言的关键字声明一个函数然后调用它,如果需要把函数作为参数传给另一个函数,或是赋值给一个本地变量,又或是作 ...

  7. JavaScript世界的一等公民 - 函数

    简介 在很多传统语言(C/C++/Java/C#等)中,函数都是作为一个二等公民存在,你只能用语言的关键字声明一个函数然后调用它,如果需要把函数作为参数传给另一个函数,或是赋值给一个本地变量,又或是作 ...

  8. javascript语言中的一等公民-函数

    简介 在很多传统语言(C/C++/Java/C#等)中,函数都是作为一个二等公民存在,你只能用语言的关键字声明一个函数然后调用它,如果需要把函数作为参数传给另一个函数,或是赋值给一个本地变量,又或是作 ...

  9. JavaScript深入浅出第2课:函数是一等公民是什么意思呢?

    摘要: 听起来很炫酷的一等公民是啥? <JavaScript深入浅出>系列: JavaScript深入浅出第1课:箭头函数中的this究竟是什么鬼? JavaScript深入浅出第2课:函 ...

随机推荐

  1. Cookie和登录注册

    1. 什么是Cookie? 服务器通过 Set-Cookie 头给客户端一串字符串 客户端每次访问相同域名的网页时,必须带上这段字符串 客户端要在一段时间内保存这个Cookie Cookie 默认在用 ...

  2. 如何解决 ajax跨域被阻止 CORS 头缺少 'Access-Control-Allow-Origin'的问题?

    已拦截跨源请求:同源策略禁止读取位于 http://192.168.1.72:8080/securityMonitor_TV/service/getTest 的远程资源. (原因:CORS 头缺少 ' ...

  3. 每日一个linux命令2

    cd命令 Linux cd命令可以说是Linux中最基本的命令语句,其他的命令语句要进行操作,都是建立在使用cd命令的基础之上. 1. 命令格式 cd [目录名] 2.命令功能 切换当前目录至dirN ...

  4. 域名解析 看Cname 信息

    CMD 命令: nslookup -q=cname www.yuzhentan.com

  5. Java NIO 文件通道 FileChannel 用法

    FileChannel 提供了一种通过通道来访问文件的方式,它可以通过带参数 position(int) 方法定位到文件的任意位置开始进行操作,还能够将文件映射到直接内存,提高大文件的访问效率.本文将 ...

  6. node中 path.join 和 path.resovle 区别

    console.log(__dirname) console.log('----') console.log(path.resolve(__dirname, '/a/b', '../')) conso ...

  7. 浅谈localStorage的使用场景和优劣势,以及sessionStorage和cookie

    一.localStorage,sessionStorage,cookie的简单介绍 localStorage:仅在客户端存储不参与服务器通信,存储大小一般为5M,如果不是人为清除,那么即使是关闭浏览器 ...

  8. 【C++】《C++ Primer 》第十章

    第十章 泛型算法 一.概述 因为它们实现共同的操作,所以称之为"算法".而"泛型",指的是它们可以操作在多种容器类型上. 泛型算法并不直接操作容器,而是遍历由两 ...

  9. python学习笔记 | PyCharm出现卡顿解决方法

    问题:使用pycharm时常出现 the IDE is running low on memory 的问题 表示pycharm这款IDE使用内存不足 需要在系统内存充足的情况下扩充IDE memory ...

  10. (二)数据源处理5-excel数据转换实战(上)

    把excel_oper02.py 里面实现的:通过字典的方式获取所有excel数据.放进utils: ️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️ utils: def get_al ...