File tree Expand file tree Collapse file tree 1 file changed +15
-6
lines changed Expand file tree Collapse file tree 1 file changed +15
-6
lines changed Original file line number Diff line number Diff line change 4
4
5
5
package errors
6
6
7
+ import (
8
+ "unsafe"
9
+ )
10
+
7
11
// Join returns an error that wraps the given errors.
8
12
// Any nil error values are discarded.
9
13
// Join returns nil if every value in errs is nil.
@@ -38,14 +42,19 @@ type joinError struct {
38
42
}
39
43
40
44
func (e * joinError ) Error () string {
41
- var b []byte
42
- for i , err := range e .errs {
43
- if i > 0 {
44
- b = append (b , '\n' )
45
- }
45
+ // Since Join returns nil if every value in errs is nil,
46
+ // e.errs cannot be empty.
47
+ if len (e .errs ) == 1 {
48
+ return e .errs [0 ].Error ()
49
+ }
50
+
51
+ b := []byte (e .errs [0 ].Error ())
52
+ for _ , err := range e .errs [1 :] {
53
+ b = append (b , '\n' )
46
54
b = append (b , err .Error ()... )
47
55
}
48
- return string (b )
56
+ // At this point, b has at least one byte '\n'.
57
+ return unsafe .String (& b [0 ], len (b ))
49
58
}
50
59
51
60
func (e * joinError ) Unwrap () []error {
You can’t perform that action at this time.
0 commit comments