diff --git a/join.go b/join.go deleted file mode 100644 index d1c2a53..0000000 --- a/join.go +++ /dev/null @@ -1,59 +0,0 @@ -//go:build !go1.20 -// +build !go1.20 - -package errors - -import "unsafe" - -// Join returns an error that wraps the given errors. -// Any nil error values are discarded. -// Join returns nil if every value in errs is nil. -// The error formats as the concatenation of the strings obtained -// by calling the Error method of each element of errs, with a newline -// between each string. -// -// A non-nil error returned by Join implements the Unwrap() []error method. -func Join(errs ...error) error { - n := 0 - for _, err := range errs { - if err != nil { - n++ - } - } - if n == 0 { - return nil - } - e := &joinError{ - errs: make([]error, 0, n), - } - for _, err := range errs { - if err != nil { - e.errs = append(e.errs, err) - } - } - return e -} - -type joinError struct { - errs []error -} - -func (e *joinError) Error() string { - // Since Join returns nil if every value in errs is nil, - // e.errs cannot be empty. - if len(e.errs) == 1 { - return e.errs[0].Error() - } - - b := []byte(e.errs[0].Error()) - for _, err := range e.errs[1:] { - b = append(b, '\n') - b = append(b, err.Error()...) - } - // At this point, b has at least one byte '\n'. - return unsafe.String(&b[0], len(b)) -} - -func (e *joinError) Unwrap() []error { - return e.errs -} diff --git a/join_go120.go b/join_go120.go index ecf930c..575d620 100644 --- a/join_go120.go +++ b/join_go120.go @@ -5,4 +5,16 @@ package errors import "errors" -var Join = errors.Join +// Join returns an error that wraps the given errors. +// Any nil error values are discarded. +// Join returns nil if every value in errs is nil. +// The error formats as the concatenation of the strings obtained +// by calling the Error method of each element of errs, with a newline +// between each string. +// +// A non-nil error returned by Join implements the Unwrap() []error method. +// +// Available only for go 1.20 or superior. +func Join(errs ...error) error { + return errors.Join(errs...) +} diff --git a/join_test.go b/join_go120_test.go similarity index 95% rename from join_test.go rename to join_go120_test.go index bfc5667..cb8b25a 100644 --- a/join_test.go +++ b/join_go120_test.go @@ -1,3 +1,6 @@ +//go:build go1.20 +// +build go1.20 + package errors_test import (