Skip to content

Instantly share code, notes, and snippets.

@ORBAT
Created March 1, 2016 14:26
Show Gist options
  • Save ORBAT/5c410e4fab15945b2cf5 to your computer and use it in GitHub Desktop.
Save ORBAT/5c410e4fab15945b2cf5 to your computer and use it in GitHub Desktop.
Golang snippets
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