Created
March 1, 2016 14:26
-
-
Save ORBAT/5c410e4fab15945b2cf5 to your computer and use it in GitHub Desktop.
Golang snippets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ( | |
"fmt" | |
"strings" | |
) | |
// MultiError holds multiple errors | |
type MultiError []error | |
// Error implements the error interface. The returned string is not memoized, so calling | |
// this method ranges over the error slice and allocates a new string | |
func (me MultiError) Error() string { | |
nErrs := len(me) | |
errStrs := make([]string, nErrs) | |
for i := range me { | |
errStrs[i] = me[i].Error() | |
} | |
return fmt.Sprintf("%d error(s): '%s'", nErrs, strings.Join(errStrs, "', '")) | |
} | |
// Append appends a new error onto the backing slice | |
func (me *MultiError) Append(err ...error) { | |
*me = append(*me, err...) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment