<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 "fmt"
import "sync"</p>
<p>func Prueba(ch chan string) {
go func() {
for{
v := <-ch
fmt.Println("recibiendo ", v)
}
}()
}</p>
<p>func main() {
var bloque sync.WaitGroup
bloque.Add(1)</p>
<pre><code>var d = make(chan string)
Prueba(d)
d <- string(3)
d <- "prueba"
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
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传