result

package
v0.0.0-...-9e0c95a Latest Latest
Warning

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

Go to latest
Published: Aug 17, 2024 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package result provides concurrency utilities for accumulating results and managing errors.

Example

import (
	result "github.com/neurocode-io/go-pkgs/result"
	"context"
)

func worker() ([]int, error) {
	// worker logic here
	return []int{1, 2, 3}, nil
}

ctx := context.Background()
group, ctx := result.WithErrorsThreshold[int](ctx, 2)
group.Go(worker)
results, err := group.Wait()
if err != nil {
	handle error
}
results == []int{1, 2, 3}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Group

type Group[T any] struct {
	// contains filtered or unexported fields
}

func WithErrorsThreshold

func WithErrorsThreshold[T any](ctx context.Context, threshold int) (Group[T], context.Context)

WithErrorsThreshold initializes a new Group[T] with a threshold for error tolerance.

Example

ctx := context.Background()
group, ctx := result.WithErrorsThreshold[int](ctx, 2)
// group is now ready to execute with a threshold of 2 errors

func (*Group[T]) Go

func (g *Group[T]) Go(f func() ([]T, error))

Go starts a goroutine that performs a given function and handles its results and errors.

Example

group.Go(func() ([]int, error) {
	return []int{1, 2, 3}, nil
})
// The results will be accumulated and errors managed based on the Group's settings.

func (*Group[T]) Wait

func (g *Group[T]) Wait() ([]T, errorWithUnwrap)

Wait blocks until all tasks have completed and returns the accumulated results and any errors.

Example

results, err := group.Wait()
if err != nil {
	// handle error
}
// results contains all accumulated results from the group operations

Jump to

Keyboard shortcuts

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