Better structured concurrency for go.
$ go get github.com/lovelysunlight/conc
Use WaitGroup
to run goroutine.
import (
"context"
"fmt"
"time"
"github.com/lovelysunlight/conc/pool"
)
func main() {
wg := conc.New(context.Background())
for i := 0; i < 10; i++ {
wg.Go(func(ctx context.Context) error {
if i >= 0 && i <= 8 {
time.Sleep(3 * time.Second)
}
fmt.Printf("hello world from %d \n", i)
return nil
})
}
_ = wg.Wait()
}
Use pool.New
to create a goroutine pool.
import (
"context"
"fmt"
"time"
"github.com/lovelysunlight/conc/pool"
)
func main() {
p := pool.New(context.Background(), pool.WithMaxGoroutines(5))
for i := 0; i < 10; i++ {
p.Go(func(context.Context) error {
if i >= 0 && i <= 8 {
time.Sleep(3 * time.Second)
}
fmt.Printf("hello world from %d \n", i)
return nil
})
}
_ = p.Wait()
}
You can contribute in one of three ways:
- File bug reports using the issue tracker.
- Answer questions or fix bugs on the issue tracker.
- Contribute new features or update the wiki.
MIT