cancelgroup

package
v1.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 27, 2026 License: Apache-2.0 Imports: 2 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package cancelgroup provides a synchronization primitive similar to sync.WaitGroup, but with built-in support for cancellation via context.Context.

It is useful for managing a set of goroutines that need to be canceled and waited for as a group.

Example

package main

import (
	"fmt"
	"time"

	"github.com/your/module/cancelgroup"
)

func main() {
	g := cancelgroup.New()

	// Start a worker
	g.Go(func() {
		for {
			select {
			case <-g.Context().Done():
				fmt.Println("worker stopped")
				return
			default:
				fmt.Println("worker running...")
				time.Sleep(200 * time.Millisecond)
			}
		}
	})

	time.Sleep(1 * time.Second)

	// Cancel all workers
	g.Cancel()

	// Wait for them to stop
	g.Wait()
	fmt.Println("all workers stopped")
}

Package cancelgroup provides a WaitGroup-like synchronization primitive that also supports cancellation via context.Context.

It allows you to run goroutines that can be canceled as a group, making it easier to coordinate shutdown and resource cleanup.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Group

type Group struct {
	// contains filtered or unexported fields
}

Group is similar to sync.WaitGroup but comes with a cancellable context. It can be used to launch goroutines, wait for their completion, and cancel them all at once.

func New

func New() *Group

New creates a new Group with its own cancellable context.

func WithContext

func WithContext(parent context.Context) *Group

WithContext creates a new Group with a parent context. Cancelling the parent will also cancel the group.

func (*Group) Add

func (g *Group) Add(delta int)

Add adds delta to the Group's counter (same as sync.WaitGroup.Add).

func (*Group) Cancel

func (g *Group) Cancel()

Cancel signals cancellation to all goroutines launched under this group.

func (*Group) Context

func (g *Group) Context() context.Context

Context returns the cancellable context associated with the group.

func (*Group) Done

func (g *Group) Done()

Done decrements the Group's counter (same as sync.WaitGroup.Done).

func (*Group) Go

func (g *Group) Go(f func())

Go runs the given function in a new goroutine and tracks it in the group. It's a convenience method equivalent to calling Add(1) before launching the goroutine and Done() when it finishes.

The function f should respect the group's context for cancellation and must not panic.

func (*Group) Wait

func (g *Group) Wait()

Wait blocks until the Group's counter goes back to zero.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL