Please explain this code (channels)

xuanbao · · 510 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I can&#39;t understand howcome only the loop part of total() function being run 3 times not the whole function. res := 0 runs only once while range part runs 3 times. My background is in Python and Java and the way total function is run not making sense at all. </p> <p><a href="http://pastebin.com/4fEvfGsU" rel="nofollow">http://pastebin.com/4fEvfGsU</a></p> <hr/>**评论:**<br/><br/>kl0nos: <pre><p>Because range on channel blocks if there is nothing to receive. You need to learn about channels in Go.</p></pre>hayzeus: <pre><p>Read up on channels and goprocs. You have started a background function (total()), which is going to read from channel <code>in</code>, adding to <code>res</code> until <code>in</code> is closed. Then the for loop end and the result is sent on the <code>out</code> channel back to <code>main</code></p> <p><code>total</code> is NOT run three times. </p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

510 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传