To add to what others have said, this code seems a little odd to me. Your
function f is called synchronously within the loop so there's no need to
use a WaitGroup or send on a done channel - when it returns you know that
it's done.

Your code would work fine without using the done channel or the waitgroup
at all.

Also your for loop in f would usually be written as the equivalent  "for i
:= range ints".

On 1 Sep 2016 19:04, "Darren Hoo" <darren....@gmail.com> wrote:

> var wg sync.WaitGroup
>
> ints := make(chan int, 1)
> done := make(chan bool)
>
> go func() {
> for i := 0; i < 3; i++ {
> ints <- i
> }
> close(ints)
> }()
>
> f := func() {
> wg.Add(1)
> defer wg.Done()
>
> for {
> i, ok := <-ints
> if !ok {
> done <- true
> return
> }
> }
> }
>
> exit:
> for {
> select {
> case <-done:
> break exit
> default:
> f()
> }
> }
> wg.Wait()
>
>
>
> --
> 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+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to