From f46d400df055a6da0925264e3b6532563ece7236 Mon Sep 17 00:00:00 2001 From: Tom Wieczorek Date: Wed, 18 May 2022 16:18:51 +0200 Subject: [PATCH] Use append instead of copy to clone slices (#58) The append notation is more concise and should be more efficient, since the target slice won't be initialized with zero values first. --- error.go | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/error.go b/error.go index f45af14..8b6fa9c 100644 --- a/error.go +++ b/error.go @@ -209,10 +209,7 @@ func Errors(err error) []error { return []error{err} } - errors := eg.Errors() - result := make([]error, len(errors)) - copy(result, errors) - return result + return append(([]error)(nil), eg.Errors()...) } // multiError is an error that holds one or more errors. @@ -393,8 +390,7 @@ func fromSlice(errors []error) error { // Otherwise "errors" escapes to the heap // unconditionally for all other cases. // This lets us optimize for the "no errors" case. - out := make([]error, len(errors)) - copy(out, errors) + out := append(([]error)(nil), errors...) return &multiError{errors: out} } }