Skip to content

lovelysunlight/conc

Repository files navigation

Conc

Build Badge Go Reference License Badge

Better structured concurrency for go.

Installing

$ go get github.com/lovelysunlight/conc

Usage

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()
}

Contributing

You can contribute in one of three ways:

  1. File bug reports using the issue tracker.
  2. Answer questions or fix bugs on the issue tracker.
  3. Contribute new features or update the wiki.

License

MIT

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published