1
0
mirror of https://github.com/golang/go synced 2024-11-23 04:50:06 -07:00

errors: optimize *joinError's Error

Remove TODOs, stale comment.
This commit is contained in:
Jes Cok 2023-06-07 07:11:48 +08:00
parent e9ffff493a
commit ed8003bfbc

View File

@ -44,24 +44,17 @@ type joinError struct {
func (e *joinError) Error() string {
// Since Join returns nil if every value in errs is nil,
// e.errs cannot be empty.
// TODO: get rid of case 0
switch len(e.errs) {
case 0: // Impossible but handle.
return "<nil>"
case 1:
if len(e.errs) == 1 {
return e.errs[0].Error()
}
// To avoid doubling the number of Error calls, skip preallocating
// the slice on overflow and let the runtime handle the panic.
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'.
// TODO: replace with unsafe.String(&b[0], len(b))
return unsafe.String(unsafe.SliceData(b), len(b))
return unsafe.String(&b[0], len(b))
}
func (e *joinError) Unwrap() []error {