Two channels are reasonable solution, here's an example: http://play.golang.org/p/sKI05-7v12
Hi, we have the same problem of OP.
But, in the Chris's playground there could be an error, indeed if the consumerruns slower of the producer the program ends in a deadlock.To force this behavior just add a time.Sleep() after the foor loop in main.
--> https://play.golang.org/p/A3i6TEyGQm_LAm I wrong?
Thanks for the help.
Il giorno martedì 21 febbraio 2012 01:51:55 UTC+1, Jan ha scritto:hi all, quick question, I have a scenario where the producer should generate numbers indefinitely, until it is told to stop.Most of the channel producer/consumer examples assume that the producer closes the channels and dies cleanly (on its goroutine).When I close the channel on the reader instead, i get a panic. Any simple ways around it ?Pseudo-code:func producer() chan int {c := make(chan int)for {// produce numberc <- some_number}}func main() {c := producer()for some_condition {consume(<-c)}close(c)...}Since I'm going to call this producer zillions of times, I want to make sure whatever memory/resources it uses is reclaimed and cleanly exited when c is closed.Ideas ?many thanks in advance :)Jan
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.