Documentation
¶
Overview ¶
Package file provides IO operations for file system interactions.
This package offers functional wrappers around common file operations, returning IO monads that encapsulate side effects. All operations are lazy and only execute when the returned IO is invoked.
Core Operations ¶
The package provides two main operations:
- Close: Safely close io.Closer resources
- Remove: Remove files from the file system
Both operations ignore errors and return the original input, making them suitable for cleanup operations where errors should not interrupt the flow.
Basic Usage ¶
// Close a file
file, _ := os.Open("data.txt")
closeIO := file.Close(file)
closeIO() // Closes the file, ignoring any error
// Remove a file
removeIO := file.Remove("temp.txt")
removeIO() // Removes the file, ignoring any error
Composition with IO ¶
These operations can be composed with other IO operations:
result := pipe.Pipe2(
openFile("data.txt"),
io.ChainFirst(processFile),
io.Chain(file.Close),
)
Error Handling ¶
Both Close and Remove intentionally ignore errors. This design is suitable for cleanup operations where:
- The operation is best-effort
- Errors should not interrupt the program flow
- The resource state is not critical
For operations requiring error handling, use ioeither or ioresult instead.
Example (Close) ¶
Example_close demonstrates basic usage of Close
// Create a mock closer
mock := &mockCloser{}
// Create an IO that closes the resource
closeIO := Close(mock)
// Execute the IO
result := closeIO()
fmt.Printf("Closed: %v\n", result.closed)
Output: Closed: true
Example (CloseAndRemove) ¶
Example_closeAndRemove demonstrates using Close and Remove together
// Create a temporary file
tmpFile, _ := os.CreateTemp("", "example-*.txt")
// Create a pipeline that closes and removes the file
pipeline := IO.Chain(func(f *os.File) IO.IO[string] {
return Remove(f.Name())
})(Close(tmpFile))
// Execute the pipeline
path := pipeline()
// Check if file exists
_, err := os.Stat(path)
fmt.Printf("File removed: %v\n", os.IsNotExist(err))
Output: File removed: true
Example (Remove) ¶
Example_remove demonstrates basic usage of Remove
// Create a temporary file
tmpFile, _ := os.CreateTemp("", "example-*.txt")
tmpPath := tmpFile.Name()
tmpFile.Close()
// Create an IO that removes the file
removeIO := Remove(tmpPath)
// Execute the IO
path := removeIO()
// Check if file exists
_, err := os.Stat(path)
fmt.Printf("File removed: %v\n", os.IsNotExist(err))
Output: File removed: true
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Close ¶
Close closes a closeable resource and ignores any potential error. Returns an IO that, when executed, closes the resource and returns it.
This function is useful for cleanup operations where errors can be safely ignored, such as in defer statements or resource cleanup chains.
Type Parameters:
- R: Any type that implements io.Closer
Parameters:
- r: The resource to close
Returns:
- IO[R]: An IO computation that closes the resource and returns it
Example:
file, _ := os.Open("data.txt")
defer file.Close(file)() // Close when function returns
Example with IO composition:
result := pipe.Pipe3(
openFile("data.txt"),
io.Chain(readContent),
io.ChainFirst(file.Close),
)
Note: The #nosec comment is intentional - errors are deliberately ignored for cleanup operations where failure should not interrupt the flow.
func Remove ¶
Remove removes a file or directory and ignores any potential error. Returns an IO that, when executed, removes the named file or directory and returns the name.
This function is useful for cleanup operations where errors can be safely ignored, such as removing temporary files or cache directories.
Parameters:
- name: The path to the file or directory to remove
Returns:
- IO[string]: An IO computation that removes the file and returns the name
Example:
cleanup := file.Remove("temp.txt")
cleanup() // Removes temp.txt, ignoring any error
Example with multiple files:
cleanup := pipe.Pipe2(
file.Remove("temp1.txt"),
io.ChainTo(file.Remove("temp2.txt")),
)
cleanup() // Removes both files
Example in defer:
tempFile := "temp.txt" defer file.Remove(tempFile)() // ... use tempFile ...
Note: The #nosec comment is intentional - errors are deliberately ignored for cleanup operations where failure should not interrupt the flow. This function only removes the named file or empty directory. To remove a directory and its contents, use os.RemoveAll wrapped in an IO.
Types ¶
This section is empty.