Please explain this code (channels)

xuanbao · 2016-04-16 03:17:43 · 668 次点击    
这是一个分享于 2016-04-16 03:17:43 的资源,其中的信息可能已经有所发展或是发生改变。

I can'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.

http://pastebin.com/4fEvfGsU


评论:

kl0nos:

Because range on channel blocks if there is nothing to receive. You need to learn about channels in Go.

hayzeus:

Read up on channels and goprocs. You have started a background function (total()), which is going to read from channel in, adding to res until in is closed. Then the for loop end and the result is sent on the out channel back to main

total is NOT run three times.


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

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