Skip to content

concgroup provides almost the same features to goroutine groups as golang.org/x/sync/errgroup but ensures sequential run of goroutines with the same group key.

License

Notifications You must be signed in to change notification settings

k1LoW/concgroup

Repository files navigation

concgroup

Go Reference CI Coverage Code to Test Ratio Test Execution Time

concgroup provides almost the same features to goroutine groups as golang.org/x/sync/errgroup but ensures sequential run of goroutines with the same group key.

Usage

package main

import (
	"fmt"
	"net/http"
	"time"

	"github.com/k1LoW/concgroup"
)

func main() {
	cg := new(concgroup.Group)
	var urlgroups = map[string][]string{
		"go": {
			"https://go.dev/",
			"https://go.dev/dl/",
		},
		"google": {
			"http://www.google.com/",
		},
	}
	for key, ug := range urlgroups {
	    key := key
		for _, url := range ug {
			url := url // https://golang.org/doc/faq#closures_and_goroutines
			cg.Go(key, func() error {
				// Fetch URL sequentially by key
				resp, err := http.Get(url)
				if err == nil {
					resp.Body.Close()
				}
				return err
			})
		}
	}
	// Wait for all HTTP fetches to complete.
	if err := cg.Wait(); err == nil {
		fmt.Println("Successfully fetched all URLs.")
	}
}

About

concgroup provides almost the same features to goroutine groups as golang.org/x/sync/errgroup but ensures sequential run of goroutines with the same group key.

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published