Skip to content

mind1949/cancelchain

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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