commit | 8deb4d8f8484161d95e127ebafbf8ad88f90f736 | [log] [tgz] |
---|---|---|
author | Prashant Varanasi <github@prashantv.com> | Mon Apr 10 15:02:29 2017 -0700 |
committer | GitHub <noreply@github.com> | Mon Apr 10 15:02:29 2017 -0700 |
tree | f96f2d5d78b4a5b75f5b153f5d1b29692064462c | |
parent | 737b41aa3bf31c25d11dc84d5275af6bfe2ef4d2 [diff] |
Avoid allocating a new slice on every Append (#9) Currently, appending a non-nil error to a multierror always copies the underlying slice, since there may be multiple goroutines appending to the same multierr, ```go merr := multierr.Append(errors.New("initial"), errors.New("multierr")) go func() { err1 = multierr.Append(merr, errors.New("1")) [..] }() go func() { err2 = multierr.Append(merr, errors.New("2")) [..] }() ``` However, the common use case is a standard for loop: ```go var merr error for _, v := range values { merr = multierr.Append(merr, processValue(v)) } return merr ``` Since there is only a single resulting slice, we don't need to create a full copy on every `Append`. Instead, we track whether we have modified the underlying slice from `Append` already using an atomic boolean, and only create a copy if the underlying slice has been modified.
multierr
allows combining one or more Go error
s together.
go get -u go.uber.org/multierr
Beta
Released under the MIT License.