Golang Limit Max Number goroutines
package main
import (
"fmt"
"sync"
)
var wg sync.WaitGroup
func main() {
const max = 10
queue := make(chan int, max)
for i := 0; i < max; i++ {
wg.Add(1)
queue <- 1
go worker(queue)
}
close(queue)
wg.Wait()
fmt.Println("Done")
}
func worker(queue chan int) {
for job := range queue {
fmt.Print(job, " ") // a job
}
<-queue
defer wg.Done()
}
smjure