Documentation
¶
Overview ¶
Package safeio provides helpers to safely drain and close readers.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CloseWithErr ¶
CloseWithErr closes c and stores close error in *dstErr if not already set.
Example ¶
var err = errPrimaryOp
// We still want to close resources even if the primary operation failed
closer := &mockCloser{}
CloseWithErr(&err, closer)
// The original error is preserved
fmt.Printf("Final error: %v\n", err)
Output: Final error: primary operation failed
Example (PreserveFirstError) ¶
var err error
// Successful primary operation (err is nil)
// But closer fails
closer := &mockCloser{closeErr: errCloseExample}
CloseWithErr(&err, closer)
// The close error is now stored in err
fmt.Printf("Final error: %v\n", err)
Output: Final error: close failed
func DrainAndClose ¶
func DrainAndClose(rc io.ReadCloser) error
DrainAndClose drains all data from rc and then closes it.
Example ¶
Example usage tests to demonstrate the API.
// Simulate a reader with some data
reader := strings.NewReader("some data to drain")
rc := &mockReadCloser{reader: reader}
err := DrainAndClose(rc)
if err != nil {
fmt.Printf("Error: %v\n", err)
return
}
fmt.Println("Successfully drained and closed")
Output: Successfully drained and closed
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.