Prechádzať zdrojové kódy

Added cause interface trait

Dave Cheney 10 rokov pred
rodič
commit
4dd713cae9
2 zmenil súbory, kde vykonal 11 pridanie a 3 odobranie
  1. 9 0
      errors.go
  2. 2 3
      errors_test.go

+ 9 - 0
errors.go

@@ -33,3 +33,12 @@ func Cause(err error) error {
 	}
 	return err
 }
+
+// cause implements the interface required by Cause.
+type cause struct {
+	err error
+}
+
+func (c *cause) Cause() error {
+	return c.err
+}

+ 2 - 3
errors_test.go

@@ -38,11 +38,10 @@ type nilError struct{}
 func (nilError) Error() string { return "nil error" }
 
 type causeError struct {
-	err error
+	cause
 }
 
 func (e *causeError) Error() string { return "cause error" }
-func (e *causeError) Cause() error  { return e.err }
 
 func TestCause(t *testing.T) {
 	tests := []struct {
@@ -66,7 +65,7 @@ func TestCause(t *testing.T) {
 		want: io.EOF,
 	}, {
 		// caused error returns cause
-		err:  &causeError{err: io.EOF},
+		err:  &causeError{cause{err: io.EOF}},
 		want: io.EOF,
 	}}