教程集 www.jiaochengji.com
教程集 >  Golang编程  >  golang教程  >  正文 android 读写串口中文字符串_golang面试题:对已经关闭的的chan进行读写,会怎么样?为什么?...

android 读写串口中文字符串_golang面试题:对已经关闭的的chan进行读写,会怎么样?为什么?...

发布时间:2022-01-15   编辑:jiaochengji.com
教程集为您提供android 读写串口中文字符串,golang面试题:对已经关闭的的chan进行读写,会怎么样?为什么?...等资源,欢迎您收藏本站,我们将为您提供最新的android 读写串口中文字符串,golang面试题:对已经关闭的的chan进行读写,会怎么样?为什么?...资源
<figure style="text-align:center;"></figure><h2><span style="font-weight:bold;"/><span style="font-weight:bold;">问题</span><span style="font-weight:bold;"/><span style="font-weight:bold;"> </span></h2>

已经关闭的的 <code>chan</code> 进行读写,会怎么样?为什么?

<h2><span style="font-weight:bold;"/><span style="font-weight:bold;">怎么答</span><span style="font-weight:bold;"/><span style="font-weight:bold;"> </span></h2> <ul><li>读已经关闭的 <code>chan</code> 能一直读到东西,但是读到的内容根据通道内<code>关闭前</code>是否有元素而不同。 <ul><li>如果 <code>chan</code> 关闭前,<code>buffer</code> 内有元素还未读 , 会正确读到 <code>chan</code> 内的值,且返回的第二个 bool 值(是否读成功)为 <code>true</code>。</li><li>如果 <code>chan</code> 关闭前,<code>buffer</code> 内有元素已经被读完,<code>chan</code> 内无值,接下来所有接收的值都会非阻塞直接成功,返回 <code>channel</code> 元素的零值,但是第二个 <code>bool</code> 值一直为 <code>false</code>。</li></ul></li><li>写已经关闭的 <code>chan</code> 会 <code>panic</code></li></ul><h2><span style="font-weight:bold;"/><span style="font-weight:bold;">举例</span><span style="font-weight:bold;"/><span style="font-weight:bold;"> </span></h2> <h5><span style="font-weight:bold;"/><span style="font-weight:bold;">1. 写已经关闭的 chan</span><span style="font-weight:bold;"/></h5> <figure style="text-align:center;"></figure><ul><li>注意这个 <code>send on closed channel</code>,待会会提到。</li></ul><h5><span style="font-weight:bold;"/><span style="font-weight:bold;">2. 读已经关闭的 chan</span><span style="font-weight:bold;"/></h5> <figure style="text-align:center;"></figure><h2><span style="font-weight:bold;"/><span style="font-weight:bold;">多问一句</span><span style="font-weight:bold;"/><span style="font-weight:bold;"> </span></h2>

1. 为什么写已经关闭的 <code>chan</code> 就会 <code>panic</code> 呢?

<ul><li>当 <code>c.closed != 0</code> 则为通道关闭,此时执行写,源码提示直接 panic,输出的内容就是上面提到的 <code>"send on closed channel"</code>。</li></ul>

2. 为什么读已关闭的 <code>chan</code> 会一直能读到值?

<ul><li><code>c.closed != 0 && c.qcount == 0</code> 指通道已经关闭,且缓存为空的情况下(已经读完了之前写到通道里的值)</li><li>如果接收值的地址 <code>ep</code> 不为空 <ul><li>那接收值将获得是一个该类型的零值</li><li><code>typedmemclr</code> 会根据类型清理相应地址的内存</li><li>这就解释了上面代码为什么关闭的 <code>chan</code> 会返回对应类型的零值</li></ul></li></ul><h4><span style="font-weight:bold;"/><span style="font-weight:bold;">文章推荐:</span><span style="font-weight:bold;"/></h4> <ul><li>对未初始化的的 chan 进行读写,会怎么样?为什么?</li><li>golang 面试题:reflect(反射包)如何获取字段 tag?为什么 json 包不能导出私有变量的 tag?</li><li>golang 面试题:json 包变量不加 tag 会怎么样?</li><li>golang 面试题:怎么避免内存逃逸?</li><li>golang 面试题:简单聊聊内存逃逸?</li><li>golang 面试题:字符串转成 byte 数组,会发生内存拷贝吗?</li><li>golang 面试题:翻转含有<code>中文、数字、英文字母</code>的字符串</li><li>golang 面试题:拷贝大切片一定比小切片代价大吗?</li><li>golang 面试题:能说说 uintptr 和 unsafe.Pointer 的区别吗?</li></ul><h6><span style="font-weight:bold;"/><span style="font-weight:bold;">如果你想每天学习一个知识点?</span><span style="font-weight:bold;"/></h6> <figure style="text-align:center;"></figure>
到此这篇关于“android 读写串口中文字符串_golang面试题:对已经关闭的的chan进行读写,会怎么样?为什么?...”的文章就介绍到这了,更多文章或继续浏览下面的相关文章,希望大家以后多多支持JQ教程网!

您可能感兴趣的文章:
android 读写串口中文字符串_golang面试题:对已经关闭的的chan进行读写,会怎么样?为什么?...
golang for循环_golang面试官:for select时,如果通道已经关闭会怎么样?如果select中只有一个case呢?...
c builder case内是局部变量吗_golang面试官:for select时,如果通道已经关闭会怎么样?如果只有一个...
golang面试官:for select时,如果通道已经关闭会怎么样?如果select中只有一个case呢?
不要等离职了,才知道for select时,如果通道已经关闭会怎么样?
连nil切片和空切片一不一样都不清楚?那BAT面试官只好让你回去等通知了
连怎么避免内存逃逸都不知道?怎么进BAT?
golang同步机制之通道
H5启动APP原生页面的实例方法
Go并发编程——channel

[关闭]
~ ~