EDIT: I wrote this answer in a hurry before I realized that your question is about sending values ββto Chan inside Goroutine. The approach below can be used either with an additional channel, as suggested above, or with the fact that you already have a bi-directional channel, you can use only one ...
If your subroutine exists solely to process elements coming from the vat, you can use the built-in close function and a special form of reception for channels.
That is, as soon as you finish sending items to the channel, you close it. Then, inside your procedure, you get an additional parameter for the receive statement, which indicates whether the channel has been closed.
Here is a complete example (a waiting group is used to make sure that the process continues until the program terminates):
package main import "sync" func main() { var wg sync.WaitGroup wg.Add(1) ch := make(chan int) go func() { for { foo, ok := <- ch if !ok { println("done") wg.Done() return } println(foo) } }() ch <- 1 ch <- 2 ch <- 3 close(ch) wg.Wait() }
laslowh Nov 11 '11 at 19:19 2011-11-11 19:19
source share