Help us shape the future of Go! Take the 2026 Go Developer Survey

failable

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 26, 2019 License: Unlicense Imports: 3 Imported by: 1

README

Failable

Perform failable operations.

done, failed := failable.Run(func(fail failable.FailFunc) {
  err := expensiveOperation()
  if err != nil {
    fail(err) // Once fail is called the execution of the operation is stopped.
  }

  nextExpensiveOperation()
})

select {
case <-done:
  // Completed Successfully
case err <-failed:
  panic(err) // The same error passed to fail
}

Documentation

Overview

Package failable provides a function that does work with failable chanels already setup.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNilFailure sent if the error passed to the fail function is nil
	ErrNilFailure = errors.New("nil failure")
)

Functions

func Do

func Do(fn func(FailFunc)) error

Do performs a run and handles the channel's appropriately.

func DoWithContext

func DoWithContext(ctx context.Context, fn HandlerFunc) error

DoWithContext performs a run and handles the channel's appropriately

func Run

func Run(fn func(FailFunc)) (Completed, Failed)

Run performs the work in a new goroutine

If the fail function is called the goroutine will exit and the failed channel will be signaled with the error.

func RunSimple

func RunSimple(fn func() error) (Completed, Failed)

RunSimple performs the function in a new goroutine and will fail with returned error if there is one.

func RunSimpleWithContext

func RunSimpleWithContext(ctx context.Context, fn SimpleHandlerFunc) (Completed, Failed)

RunSimpleWithContext performs the function in a new goroutine and will fail with returned error if there is one.

The context is passed to the function.

func RunWithContext

func RunWithContext(ctx context.Context, fn HandlerFunc) (Completed, Failed)

RunWithContext performs the work in a new goroutine passing the context into the handler function.

Types

type Completed

type Completed <-chan struct{}

Completed is the channel this is signaled when the function successfully completes.

type FailFunc

type FailFunc func(error)

FailFunc can be called to fail an operation immediately.

If the failure function is passed a nil error the failed channel will be signaled with ErrCompleted

type Failed

type Failed <-chan error

Failed is the channel this is signaled with the failing error.

type HandlerFunc

type HandlerFunc func(context.Context, FailFunc)

HandlerFunc is the main function that is called with a context and the failure function.

type SimpleHandlerFunc

type SimpleHandlerFunc func(context.Context) error

SimpleHandlerFunc allows you to directly return an error instead of calling the failure function.

Jump to

Keyboard shortcuts

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