Created
January 14, 2016 15:32
-
-
Save esimov/d0075d10825381180ce2 to your computer and use it in GitHub Desktop.
Goroutines iterator using WaitGroup
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"sync" | |
"time" | |
) | |
func main() { | |
var ch chan int | |
var wg sync.WaitGroup | |
ch = make(chan int) | |
routines := [100]int{} | |
wg.Add(len(routines)) | |
for i := 0; i < len(routines); i++ { | |
go func(i int) { | |
defer wg.Done() | |
ch <- i | |
}(i) | |
} | |
go func(ch chan int) { | |
for { | |
if ch, ok := <-ch; ok { | |
fmt.Println(ch) | |
time.Sleep(time.Second) | |
} | |
} | |
}(ch) | |
wg.Wait() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment