Debian Codegraph @

README.md

raw symbols SCIP
1 # core
2
3 [![core module docs](https://img.shields.io/badge/go.dev-reference-007d9c?logo=go&logoColor=white&style=flat-square)](https://pkg.go.dev/github.com/efficientgo/core)
4
5 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](https://github.com/davecgh/go-spew) and [google/go-cmp](https://github.com/google/go-cmp).
6
7 Maintained by experienced Go developers, including author of the [Efficient Go book](https://www.oreilly.com/library/view/efficient-go/9781098105709/).
8
9 Import it using `go get github.com/efficientgo/core@latest`.
10
11 This module contains packages around the following functionalities:
12
13 > NOTE: Click on each package to see usage examples in pkg.go.dev!
14
15 ## Error Handling
16
17 * [github.com/efficientgo/core/errors](https://pkg.go.dev/github.com/efficientgo/core/errors) is an improved and minimal version of the popular [`pkg/errors`](https://github.com/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:
18
19 ```go
20 var (
21 err = errors.New("root error while doing A")
22 wrapped = errors.Wrap(err, "while doing B")
23 )
24 ```
25
26 * [github.com/efficientgo/core/merrors](https://pkg.go.dev/github.com/efficientgo/core/merrors) implements type safe collection of multiple errors. It presentings them in a unified way as a single `error` interface.
27
28 ```go
29 func CloseAll(closers []io.Closer) error {
30 errs := merrors.New()
31 for _, c := range closers {
32 errs.Add(c.Close())
33 }
34 return errs.Err()
35 }
36 ```
37
38 * [github.com/efficientgo/core/errcapture](https://pkg.go.dev/github.com/efficientgo/core/errcapture) offers readable and robust error handling in defer statement using Go return arguments.
39
40 ```go
41 func DoAndClose(f *os.File) (err error) {
42 defer errcapture.Do(&err, f.Close, "close file at the end")
43
44 // Do something...
45 if err := do(); err != nil {
46 return err
47 }
48
49 return nil
50 }
51 ```
52
53 * [github.com/efficientgo/core/logerrcapture](https://pkg.go.dev/github.com/efficientgo/core/logerrcapture) is similar to `errcapture`, but instead of appending potential error it logs it using logger interface.
54
55 ```go
56 func DoAndClose(f *os.File, logger logerrcapture.Logger) error {
57 defer logerrcapture.Do(logger, f.Close, "close file at the end")
58
59 // Do something...
60 if err := do(); err != nil {
61 return err
62 }
63
64 return nil
65 }
66 ```
67
68 ## Waiting and Retrying
69
70 * [github.com/efficientgo/core/runutil](https://pkg.go.dev/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).
71
72 ```go
73 // Repeat every 1 second until context is done (e.g. cancel or timeout) or
74 // function returns error.
75 err := runutil.Repeat(1*time.Second, ctx.Done(), func() error {
76 // ...
77 return err // Ups, error - don't repeat anymore!
78 })
79
80 // Retry every 1 second until context is done (e.g. cancel or timeout) or
81 // function returns nil.
82 err := runutil.Retry(1*time.Second, ctx.Done(), func() error {
83 // ...
84 return nil // Done, no need to retry!
85 })
86 ```
87
88 * [github.com/efficientgo/core/backoff](https://pkg.go.dev/github.com/efficientgo/core/backoff) offers backoff timers which increases wait time on every retry, incredibly useful in distributed system timeout functionalities.
89
90 ## Testing
91
92 * [github.com/efficientgo/core/testutil](https://pkg.go.dev/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](https://github.com/stretchr/testify) project which has a bit more bloated interface and larger dependencies.
93
94 ```go
95 func TestSomething(t *testing.T) {
96 got, err := something()
97 testutil.Ok(t, err)
98 testutil.Equals(t, expected, got, "expected different thing from something")
99 }
100 ```
101
102 ## Initial Authors
103
104 * [`@bisakhmondal`](https://github.com/bisakhmondal) (`errors` package).
105 * [`@bwplotka`](https://bwplotka.dev)
106 * [`@GiedriusS`](https://github.com/GiedriusS) (`errcapture` and `logerrcapture`)