Documentation
¶
Overview ¶
Package concurrentloop provides a function to call a function concurrently powered by generics, channels, and goroutines.
Index ¶
- func ExecuteCh[T any](ctx context.Context, fns []ExecuteFunc[T]) chan ResultCh[T]
- func IsZeroOfUnderlyingType(x interface{}) bool
- func Map[T any, Result any](ctx context.Context, sl []T, f MapFunc[T, Result], opts ...Func) ([]Result, []error)
- type Errors
- type ExecuteFunc
- type Func
- type MapFunc
- type Option
- type ResultCh
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ExecuteCh ¶ added in v1.1.1
func ExecuteCh[T any](ctx context.Context, fns []ExecuteFunc[T]) chan ResultCh[T]
ExecuteCh calls the `fns` concurrently.
NOTE: It's the caller's responsibility to close the channel.
func IsZeroOfUnderlyingType ¶ added in v1.1.9
func IsZeroOfUnderlyingType(x interface{}) bool
func Map ¶ added in v1.1.1
func Map[T any, Result any]( ctx context.Context, sl []T, f MapFunc[T, Result], opts ...Func, ) ([]Result, []error)
Map concurrently applies a function `f` to each element in the slice `sl` and returns the resulting slice and any errors that occurred. `f` should be of type MapFunc, a function which takes a context and an element of type `T` and returns a result of type `Result` and an error.
The function takes an optional number of `Func` options that allow you to customize the behavior of the function.
If an error occurs during execution of `f`, it is stored and returned along with the results. The order of the results matches the order of the input slice.
If any of the operations are cancelled by the context, the function will panic.
Usage example:
type MyStruct struct { ... }
func process(ctx context.Context, s MyStruct) (ResultType, error) { ... }
sl := []MyStruct{...}
ctx := context.Background()
results, errs := Map(ctx, sl, process)
if errs != nil {
// handle errors
}
// process results
Note: Because the function executes concurrently, the functions you provide must be safe for concurrent use.
Types ¶
type Errors ¶ added in v1.1.0
type Errors []error
Errors is a slice of errors.
type ExecuteFunc ¶ added in v1.1.1
ExecuteFunc 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.
type Func ¶ added in v1.0.1
Func allows to specify message's options.
func RemoveZeroValues ¶ added in v1.1.9
RemoveZeroValues if set to true removes zero values from the results.
func WithConcurrency ¶ added in v1.1.7
WithConcurrency sets the concurrency.
type MapFunc ¶ added in v1.1.1
MapFunc 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.