Documentation
¶
Overview ¶
Package safeio provides small helpers to safely drain and close readers and to propagate close errors without losing earlier errors.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CloseWithErr ¶
CloseWithErr closes c and, if dstErr is non-nil and not already set, stores the close error into *dstErr. This helps preserve the first error while ensuring resources are closed.
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 to io.Discard and then closes rc. It returns a wrapped error if the drain or close operation fails.
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.