This could implement a simple goruntine pool.
- create a waitgroup with ‘size’
waitgroup.Create(size int)
- push ‘one’ into the group. block, if the group is full.
WaitGroup.BlockAdd()
- pop ‘one’ out the group.
WaitGroup.Done()
- waiting the group empty
WaitGroup.Wait()
package main
import (
"fmt"
"github.com/helloyi/go-waitgroup"
)
func main() {
wg := waitgroup.Create(3)
for i, v := range []int{1, 2, 3, 4, 5} {
wg.BlockAdd() // Block, If there are 3 goruntine runing, otherwise not.
go func(k, v int) {
fmt.Println(k, v)
wg.Done()
}(i, v)
}
wg.Wait()
}