aboutsummaryrefslogtreecommitdiffstats
path: root/utils/ioutil/common.go
diff options
context:
space:
mode:
Diffstat (limited to 'utils/ioutil/common.go')
-rw-r--r--utils/ioutil/common.go20
1 files changed, 20 insertions, 0 deletions
diff --git a/utils/ioutil/common.go b/utils/ioutil/common.go
index f5b78df..a847abd 100644
--- a/utils/ioutil/common.go
+++ b/utils/ioutil/common.go
@@ -62,3 +62,23 @@ func (writeNopCloser) Close() error { return nil }
func WriteNopCloser(w io.Writer) io.WriteCloser {
return writeNopCloser{w}
}
+
+// CheckClose is used with defer to close the given io.Closer and check its
+// returned error value. If Close returns an error and the given *error
+// is not nil, *error is set to the error returned by Close.
+//
+// CheckClose is typically used with named return values like so:
+//
+// func do(obj *Object) (err error) {
+// w, err := obj.Writer()
+// if err != nil {
+// return nil
+// }
+// defer CheckClose(w, &err)
+// // work with w
+// }
+func CheckClose(c io.Closer, err *error) {
+ if cerr := c.Close(); cerr != nil && *err == nil {
+ *err = cerr
+ }
+}