why my code exits with a fatal error: all goroutines are sleep??

agolangf · · 422 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Hi guys, I could found many codes than show the same error, but unfortunately all them are different to my code (in general they forgot use sync.WaitGroup and the main thread was exiting before run the goroutines)</p> <p>my code is</p> <p>package main</p> <p>import &#34;fmt&#34; import &#34;sync&#34;</p> <p>func Prueba(ch chan string) { go func() { for{ v := &lt;-ch fmt.Println(&#34;recibiendo &#34;, v) } }() }</p> <p>func main() { var bloque sync.WaitGroup bloque.Add(1)</p> <pre><code>var d = make(chan string) Prueba(d) d &lt;- string(3) d &lt;- &#34;prueba&#34; bloque.Wait() </code></pre> <p>}</p> <p>is possible fix it without do many changes in the code?..basically I want than my function receives a channel and check when new data income to my channel, thank you so much</p> <hr/>**评论:**<br/><br/>allhatenocattle: <pre><p>I put your source code into the Go playground to have it be formatted correctly <a href="http://play.golang.org/p/A3t76dv2ok" rel="nofollow">http://play.golang.org/p/A3t76dv2ok</a></p> <p>The problems are:<br/> 1) There is a Wait() call, but never a Done()<br/> 2) The Prueba function keeps reading</p> <p>Here is one take with fixing your code: <a href="http://play.golang.org/p/EkPOmCbtOE" rel="nofollow">http://play.golang.org/p/EkPOmCbtOE</a></p></pre>

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

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