simplewg

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 20, 2024 License: MIT Imports: 2 Imported by: 1

README

SimpleWG

Go Reference

Overview

SimpleWG is a Go package that provides a simplified wrapper around the standard sync.WaitGroup. It is designed to make working with goroutines more straightforward by automating the handling of sync.WaitGroup.Add and sync.WaitGroup.Done. This package is ideal for scenarios where you need to manage the lifecycle of multiple goroutines easily.

Installation

To use SimpleWG in your Go project, install it using go get:

go get github.com/point-c/simplewg

Usage

Creating a New WaitGroup

Create a new instance of Wg:

var wg simplewg.Wg
Running Goroutines

Use the Go method to run functions in separate goroutines. This method automatically handles the addition of goroutines to the waitgroup:

wg.Go(func() {
    // Your goroutine logic here
})
Waiting for Goroutines to Complete

To wait for all goroutines to complete, use the Wait method:

wg.Wait()
Wait for Goroutines to Complete with Timeout

o wait for goroutines to complete while also considering a timeout or other event, use the Done method in combination with a select statement.

var wg simplewg.Wg
// Use Go to start goroutines...
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
select {
case <-ctx.Done():
case <-wg.Done():
}

Example

Here's a simple example demonstrating the usage of the SimpleWG package:

package main

import (
    "fmt"
    "github.com/yourusername/simplewg"
)

func main() {
    var wg simplewg.Wg

    wg.Go(func() {
        fmt.Println("Hello from goroutine 1")
    })

    wg.Go(func() {
        fmt.Println("Hello from goroutine 2")
    })

    wg.Wait()
    // or
    select {
    case <-wg.Done():
    }
	
    fmt.Println("All goroutines completed")
}

Testing

The package includes tests that demonstrate its functionality. Use Go's testing tools to run the tests:

go test

Godocs

To regenerate godocs:

go generate -tags docs ./...

Documentation

Overview

Package simplewg provides a convenience wrapper for sync.WaitGroup. It automates the invocation of sync.WaitGroup.Add and sync.WaitGroup.Done.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Wg

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

Wg struct wraps sync.WaitGroup to offer enhanced concurrency control. It manages the lifecycle of goroutines and ensures synchronization between them.

func (*Wg) Done added in v0.1.0

func (wg *Wg) Done() <-chan struct{}

Done returns a channel that gets closed when all goroutines have finished. The first call to Done prevents new goroutines from starting via Go.

func (*Wg) Go

func (wg *Wg) Go(fn func()) (ok bool)

Go method initiates a function in a separate goroutine. Once Wg.Wait has been called for the first time, subsequent calls to Go will not execute the function. Returns true if the function is executed, false otherwise. Note: Handling of any panics is the responsibility of the caller. This method does not propagate panics created in the underlying goroutine.

func (*Wg) Wait

func (wg *Wg) Wait()

Wait method blocks the caller until all goroutines have completed. It is designed to handle multiple concurrent calls.

Jump to

Keyboard shortcuts

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