github.com/karrick/gorill@v1.10.3/readAllThenClose.go (about) 1 package gorill 2 3 import ( 4 "io" 5 "io/ioutil" 6 ) 7 8 // ReadAllThenClose reads all bytes from rc then closes it. It returns any 9 // errors that occurred when either reading or closing rc. 10 func ReadAllThenClose(rc io.ReadCloser) ([]byte, error) { 11 buf, rerr := ioutil.ReadAll(rc) 12 cerr := rc.Close() // always close regardless of read error 13 if rerr != nil { 14 return buf, rerr // Read error has more context than Close error 15 } 16 return buf, cerr 17 }