Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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