Documentation
¶
Overview ¶
Package concurrentloop provides a function to call a function concurrently powered by generics, channels, and goroutines.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func RunCh ¶ added in v1.1.0
RunCh calls the `Func` concurrently on each element of `sl`, and returns a channel that receives the results. The results are returned as a `resultCh` struct, which contains the output value and an error value if the function call failed.
Example usage:
resultsCh := RunCh(ctx, sl, f)
defer close(resultsCh)
var (
results []Result
errs []error
)
for range sl {
result := <-resultsCh
if result.Error != nil {
errs = append(errs, result.Error)
} else {
results = append(results, result.Output)
}
}
// Process any errors that were returned.
if len(errs) > 0 {
// Handle errors.
}
// Process the results.
// ...
NOTE: It's the caller's responsibility to close the channel.
Types ¶
type Errors ¶ added in v1.1.0
type Errors []error
Errors is a slice of errors.
func Run ¶ added in v1.0.1
Run calls the `Func` concurrently on each element of `sl`, and returns the results and any errors that occurred. The function blocks until all executions have completed.
Example usage:
results, errs := Run(ctx, sl, f)
// Process any errors that were returned.
if len(errs) > 0 {
// Handle errors.
}
// Process the results.
// ...
type Func ¶ added in v1.0.1
Func is the type of the function that will be executed concurrently for each element in a slice of type `T`. The function takes a `context.Context` and a value of type `T`, and returns a value of type `Result` and an error value.
Example usage:
func myFunc(ctx context.Context, s string) (int, error) {
// Perform some computation using the input `s`.
...
return result, err
}
var sl []string
// Add strings to `sl`.
...
results, errs := Run(ctx, sl, myFunc)