Golang utility functions to ease working with goroutines and provide functional programming utilities. This package is inspired by async and implements utility functions in an idiomatic Go way.
goutils provides around 70 functions that include the usual 'functional' suspects (map, reduce, filter, each…) as well as some common patterns for asynchronous control flow (async/await, queue, waterfall). Package goutils has sync map, reduce etc., whereas goutils/async has concurrent map, reduce etc.
Example: Using async.Async
The async.Async function allows you to execute multiple functions concurrently and wait for all of them to complete.
package main
import (
"fmt"
"time"
"github.com/skatiyar/goutils/async"
)
func taskOne () (string, error) {
time.Sleep(time.Second)
return "Task 1", nil
}
func taskTwo () (string, error) {
time.Sleep(2 * time.Second)
panic("Task 2 paniced!")
}
func taskThree () (string, error) {
time.Sleep(3 * time.Second)
return "Task 3", nil
}
func main() {
t1Result := async.Async(taskOne)
t2Result := async.Async(taskTwo)
t3Result := async.Async(taskThree)
t2Data, t2Error := t2Result.Await()
if t2Error != nil {
fmt.Printf("Error: %v\n", t2Error)
} else {
fmt.Println("Task completed successfully", t2Data)
}
t1Data, t1Error := t1Result.Await()
if t1Error != nil {
fmt.Printf("Error: %v\n", t1Error)
} else {
fmt.Println("Task completed successfully", t1Data)
}
t3Data, t3Error := t3Result.Await()
if t3Error != nil {
fmt.Printf("Error: %v\n", t3Error)
} else {
fmt.Println("Task completed successfully", t3Data)
}
}Output:
Error: panic in go routine
Task completed successfully Task 1
Task completed successfully Task 3To install the package, run:
go get github.com/skatiyar/goutilsThis project is licensed under the MIT License. See the LICENSE file for details.