Skip to content

Instantly share code, notes, and snippets.

@esimov
Created January 14, 2016 15:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save esimov/d0075d10825381180ce2 to your computer and use it in GitHub Desktop.
Save esimov/d0075d10825381180ce2 to your computer and use it in GitHub Desktop.
Goroutines iterator using WaitGroup
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