waitgroup

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 13, 2024 License: Apache-2.0 Imports: 1 Imported by: 2

README

waitgroup

Package waitgroup provides a wrapper around sync.WaitGroup to simplify launching goroutines and waiting for their completion.

Go Reference Go Report Card

Features

  • Simple wrapper around sync.WaitGroup
  • Handle goroutine launch and completion waiting in a single method
  • Keep your concurrent code cleaner

Installation

Install the package with:

go get github.com/goaux/waitgroup

Usage

Here's a basic example of how to use the package:

package main

import (
    "fmt"
    "time"

    "github.com/goaux/waitgroup"
)

func main() {
    var sy waitgroup.Sync
    results := make(chan string, 3)

    sy.Go(func() {
        time.Sleep(100 * time.Millisecond)
        results <- "First task done"
    })

    sy.Go(func() {
        time.Sleep(200 * time.Millisecond)
        results <- "Second task done"
    })

    sy.Go(func() {
        time.Sleep(50 * time.Millisecond)
        results <- "Third task done"
    })

    // Wait for all goroutines to complete
    sy.Wait()
    close(results)

    // Print results in the order they were added to the channel
    for result := range results {
        fmt.Println(result)
    }
}

Documentation

Overview

Package waitgroup provides a wrapper around sync.WaitGroup to simplify launching goroutines and waiting for their completion.

Example
package main

import (
	"fmt"
	"time"

	"github.com/goaux/waitgroup"
)

func main() {
	var sy waitgroup.Sync
	results := make(chan string, 3)

	sy.Go(func() {
		time.Sleep(100 * time.Millisecond)
		results <- "First task done"
	})

	sy.Go(func() {
		time.Sleep(200 * time.Millisecond)
		results <- "Second task done"
	})

	sy.Go(func() {
		time.Sleep(50 * time.Millisecond)
		results <- "Third task done"
	})

	// Wait for all goroutines to complete
	sy.Wait()
	close(results)

	// Print results in the order they were added to the channel
	for result := range results {
		fmt.Println(result)
	}
}
Output:
Third task done
First task done
Second task done

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Sync

type Sync struct {
	sync.WaitGroup
}

Sync is a wrapper struct around sync.WaitGroup that provides a convenient method for launching goroutines.

func (*Sync) Go

func (sy *Sync) Go(fn func())

Go launches the given function in a new goroutine and automatically handles the addition and completion signaling for the wait group. This method simplifies the process of using WaitGroup with goroutines.

Jump to

Keyboard shortcuts

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