aboutsummaryrefslogtreecommitdiffstats
path: root/utils/ioutil/common_test.go
diff options
context:
space:
mode:
authorSantiago M. Mola <santi@mola.io>2017-02-06 17:55:14 +0100
committerSantiago M. Mola <santi@mola.io>2017-02-06 17:55:14 +0100
commit70eff0d7bd1f69856f8028c2487576085a54a42c (patch)
treee19336caa8b585ce3de467a0d0116383701a8841 /utils/ioutil/common_test.go
parentc340fb9a0f1f7c025da5ffa2d1a7389a4eabaae2 (diff)
downloadgo-git-70eff0d7bd1f69856f8028c2487576085a54a42c.tar.gz
doc: improve ioutil.CheckClose doc and example, fix #246.
* Use a proper executable example, instead of one in the comment. * Improve wording of CheckClose godoc.
Diffstat (limited to 'utils/ioutil/common_test.go')
-rw-r--r--utils/ioutil/common_test.go23
1 files changed, 23 insertions, 0 deletions
diff --git a/utils/ioutil/common_test.go b/utils/ioutil/common_test.go
index f5017f7..2d6ef80 100644
--- a/utils/ioutil/common_test.go
+++ b/utils/ioutil/common_test.go
@@ -3,6 +3,7 @@ package ioutil
import (
"bytes"
"io/ioutil"
+ "strings"
"testing"
. "gopkg.in/check.v1"
@@ -53,3 +54,25 @@ func (s *CommonSuite) TestNewReadCloser(c *C) {
c.Assert(r.Close(), IsNil)
c.Assert(closer.called, Equals, 1)
}
+
+func ExampleCheckClose() {
+ // CheckClose is commonly used with named return values
+ f := func() (err error) {
+ // Get a io.ReadCloser
+ r := ioutil.NopCloser(strings.NewReader("foo"))
+
+ // defer CheckClose call with an io.Closer and pointer to error
+ defer CheckClose(r, &err)
+
+ // ... work with r ...
+
+ // if err is not nil, CheckClose will assign any close errors to it
+ return err
+
+ }
+
+ err := f()
+ if err != nil {
+ panic(err)
+ }
+}