Help us shape the future of Go! Take the 2026 Go Developer Survey

awaiter

package module
v0.0.0-...-2d2c959 Latest Latest
Warning

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

Go to latest
Published: Jul 2, 2019 License: MIT Imports: 1 Imported by: 1

README

awaiter

WaitGroup like module to await goroutines

By v-braun - viktor-braun.de.

Build Status codecov PR welcome

Description

Awaiter is similar to a WaitGroup but simplifies the resource handling.

change this:

wg := new(sync.WaitGroup)

counter := 0

wg.Add(1)
go func() {
    defer wg.Done()
    time.Sleep(time.Second * 1)
    counter += 1
}()

wg.Add(1)
go func() {
    defer wg.Done()
    time.Sleep(time.Second * 1)
    counter += 1
}()

wg.Wait()

fmt.Printf("counter: %d", counter)
// Output: counter: 2

to this:

awaiter := awaiter.New()

counter := 0

awaiter.Go(func() {
    time.Sleep(time.Second * 1)
    counter += 1
})

awaiter.Go(func() {
    time.Sleep(time.Second * 1)
    counter += 1
})

awaiter.AwaitSync()

fmt.Printf("counter: %d", counter)
// Output: counter: 2

See more examples here and the docs here

Installation

go get github.com/v-braun/awaiter

Authors

image
v-braun

Contributing

Make sure to read these guides before getting started:

License

awaiter is available under the MIT License. See LICENSE for details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Awaiter

type Awaiter interface {
	// AwaitSync blocks until all routines started with "Go"
	// are finished (same as WaitGroup.Wait)
	AwaitSync()

	// Go executes "impl" in a goroutine and increments
	// the internal WaitGroup counter by one
	Go(impl func())

	// Cancel signals the executed goroutines to cancel
	// their execution.
	// After Cancel was called, IsCancelRequested returns true and the channel
	// returned by CancelRequested is closed
	Cancel()

	// IsCancelRequested returns true if Cancel was called
	IsCancelRequested() bool

	// CancelRequested returns a channel than never provides data
	// but will be closed when Cancel was called
	CancelRequested() <-chan interface{}
}

Awaiter is similar to a WaitGroup but simplifies the resource handling

func New

func New() Awaiter

New creates a new Awaiter instance

Jump to

Keyboard shortcuts

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