mind1949 / cancelchain

CancelChain💥⛓: 轻松完成并发执行、顺序关闭goroutine

Home Page:https://github.com/mind1949/cancelchain

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

CancelChain💥⛓️

Language Documentation Go Report Card

CancelChain 提供并发原语。轻松实现并发启动、顺序取消goroutine。

示例

package main

import (
	"context"
	"fmt"
	"time"

	"github.com/mind1949/cancelchain"
)

func main() {
	ctx, cancel := context.WithTimeout(context.Background(), time.Second)
	defer cancel()

	c := cancelchain.WithContext(ctx)

	for i := 0; i < 10; i++ {
		seq := i

		c.Go(func(ctx context.Context) error {
			<-ctx.Done()
			fmt.Printf("exit goroutine[%d]\n", seq)
			return ctx.Err()
		})
	}

	err := c.Wait()
	fmt.Printf("err: %+v\n", err)
}

About

CancelChain💥⛓: 轻松完成并发执行、顺序关闭goroutine

https://github.com/mind1949/cancelchain

License:MIT License


Languages

Language:Go 100.0%