safeio

package
v0.4.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 16, 2026 License: MIT Imports: 2 Imported by: 0

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

func CloseWithErr(dstErr *error, c io.Closer)

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL