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)
my code is
package main
import "fmt" import "sync"
func Prueba(ch chan string) { go func() { for{ v := <-ch fmt.Println("recibiendo ", v) } }() }
func main() { var bloque sync.WaitGroup bloque.Add(1)
var d = make(chan string)
Prueba(d)
d <- string(3)
d <- "prueba"
bloque.Wait()
}
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
评论:
allhatenocattle:
I put your source code into the Go playground to have it be formatted correctly http://play.golang.org/p/A3t76dv2ok
The problems are:
1) There is a Wait() call, but never a Done()
2) The Prueba function keeps readingHere is one take with fixing your code: http://play.golang.org/p/EkPOmCbtOE
