v-braun / awaiter

WaitGroup like module to await goroutines

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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.

About

WaitGroup like module to await goroutines

License:MIT License


Languages

Language:Go 100.0%