core

module
v1.0.0-rc.2 Latest Latest
Warning

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

Go to latest
Published: Dec 15, 2022 License: Apache-2.0

README

core

core module docs

Go module with set of core packages every Go project needs. Minimal API, battle-tested, strictly versioned and with only two transient dependencies-- davecgh/go-spew and google/go-cmp.

Maintained by experienced Go developers, including author of the Efficient Go book.

Import it using go get "github.com/efficientgo/core@latest.

This module contains packages around the following functionalities:

NOTE: Click on each package to see usage examples in pkg.go.dev!

Error Handling

  • github.com/efficientgo/core/errors is an improved and minimal version of the popular pkg/errors package (archived) allowing reliable wrapping of errors with stacktrace. Unfortunately, the standard library recommended error wrapping using %+w is prone to errors and does not support stacktraces. For example:
var (
	err     = errors.New("root error while doing A")
	wrapped = errors.Wrap(err, "while doing B")
)  
func CloseAll(closers []io.Closer) error {
	errs := merrors.New()
	for _, c := range closers {
		errs.Add(c.Close())
	}
	return errs.Err()
}
func DoAndClose(f *os.File) (err error) {
	defer errcapture.Do(&err, f.Close, "close file at the end")

	// Do something...
	if err := do(); err != nil {
		return err
	}

	return nil
}   
func DoAndClose(f *os.File, logger logerrcapture.Logger) error {
	defer logerrcapture.Do(logger, f.Close, "close file at the end")

	// Do something...
	if err := do(); err != nil {
		return err
	}

	return nil
}   

Waiting and Retrying

  • github.com/efficientgo/core/runutil offers Retry and Repeat functions which is often need in Go production code (e.g. repeating operation periodically) as well as tests (e.g. waiting on eventual results instead of sleeping).
// Repeat every 1 second until context is done (e.g. cancel or timeout) or
// function returns error.
err := runutil.Repeat(1*time.Second, ctx.Done(), func() error {
	// ...
	return err // Ups, error - don't repeat anymore!
})

// Retry every 1 second until context is done (e.g. cancel or timeout) or
// function returns nil.
err := runutil.Retry(1*time.Second, ctx.Done(), func() error {
	// ...
	return nil // Done, no need to retry!
}) 

Testing

  • github.com/efficientgo/core/testutil is a minimal testing utility with only few functions like Assert, Ok, NotOk for errors and Equals. It's an alternative to testify project which has a bit more bloated interface and larger dependencies.
func TestSomething(t *testing.T) {
	got, err := something()
	testutil.Ok(t, err)
	testutil.Equals(t, expected, got, "expected different thing from something")
}

Initial Authors

Directories

Path Synopsis
Package backoff implements backoff timers which increases wait time on every retry, incredibly useful in distributed system timeout functionalities.
Package backoff implements backoff timers which increases wait time on every retry, incredibly useful in distributed system timeout functionalities.
Package errcapture implements robust error handling in defer statements using provided return argument.
Package errcapture implements robust error handling in defer statements using provided return argument.
Package errors provides basic utilities to manipulate errors with a useful stacktrace.
Package errors provides basic utilities to manipulate errors with a useful stacktrace.
Package logerrcapture implements robust error handling in defer statements using provided logger.
Package logerrcapture implements robust error handling in defer statements using provided logger.
Package merrors implements multi error implementation that chains errors on the same level.
Package merrors implements multi error implementation that chains errors on the same level.
Package runutil implements helpers for advanced function scheduling control like repeat or retry.
Package runutil implements helpers for advanced function scheduling control like repeat or retry.
Package testutil is a minimal testing utility with only few functions like `Assert`, `Ok`, `NotOk` for errors and `Equals`.
Package testutil is a minimal testing utility with only few functions like `Assert`, `Ok`, `NotOk` for errors and `Equals`.

Jump to

Keyboard shortcuts

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