Documentation
¶
Index ¶
- Variables
- type IOEither
- func Close[C io.Closer](c C) IOEither[error, any]
- func Mkdir(path string, perm os.FileMode) IOEither[error, string]
- func MkdirAll(path string, perm os.FileMode) IOEither[error, string]
- func ReadAll[R io.ReadCloser](acquire IOEither[error, R]) IOEither[error, []byte]
- func Remove(name string) IOEither[error, string]
- func WithTempFile[A any](f Kleisli[error, *os.File, A]) IOEither[error, A]
- type Kleisli
- type Operator
Constants ¶
This section is empty.
Variables ¶
View Source
var ( // Open opens a file for reading Open = ioeither.Eitherize1(os.Open) // Create opens a file for writing Create = ioeither.Eitherize1(os.Create) // ReadFile reads the context of a file ReadFile = ioeither.Eitherize1(os.ReadFile) // Stat returns [FileInfo] object Stat = ioeither.Eitherize1(os.Stat) // UserCacheDir returns an [IOEither] that resolves to the default root directory // to use for user-specific cached data. Users should create their own application-specific // subdirectory within this one and use that. // // On Unix systems, it returns $XDG_CACHE_HOME as specified by // https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html if // non-empty, else $HOME/.cache. // On Darwin, it returns $HOME/Library/Caches. // On Windows, it returns %LocalAppData%. // On Plan 9, it returns $home/lib/cache. // // If the location cannot be determined (for example, $HOME is not defined), // then it will return an error wrapped in [E.Left]. UserCacheDir = ioeither.Eitherize0(os.UserCacheDir)() // UserConfigDir returns an [IOEither] that resolves to the default root directory // to use for user-specific configuration data. Users should create their own // application-specific subdirectory within this one and use that. // // On Unix systems, it returns $XDG_CONFIG_HOME as specified by // https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html if // non-empty, else $HOME/.config. // On Darwin, it returns $HOME/Library/Application Support. // On Windows, it returns %AppData%. // On Plan 9, it returns $home/lib. // // If the location cannot be determined (for example, $HOME is not defined), // then it will return an error wrapped in [E.Left]. UserConfigDir = ioeither.Eitherize0(os.UserConfigDir)() // UserHomeDir returns an [IOEither] that resolves to the current user's home directory. // // On Unix, including macOS, it returns the $HOME environment variable. // On Windows, it returns %USERPROFILE%. // On Plan 9, it returns the $home environment variable. // // If the location cannot be determined (for example, $HOME is not defined), // then it will return an error wrapped in [E.Left]. UserHomeDir = ioeither.Eitherize0(os.UserHomeDir)() )
View Source
var ( // CreateTemp created a temp file with proper parametrization CreateTemp = ioeither.Eitherize2(os.CreateTemp) )
Functions ¶
This section is empty.
Types ¶
type IOEither ¶
func MkdirAll ¶
MkdirAll create a sequence of directories, see os.MkdirAll
type Kleisli ¶
func Read ¶
func Read[R any, RD io.ReadCloser](acquire IOEither[error, RD]) Kleisli[error, Kleisli[error, RD, R], R]
Read uses a generator function to create a stream, reads data from it using a provided reader function, and ensures the stream is properly closed after reading.
This function provides safe resource management for reading operations by:
- Acquiring a ReadCloser resource using the provided acquire function
- Applying a reader function to extract data from the resource
- Ensuring the resource is closed, even if an error occurs during reading
Type Parameters:
- R: The type of data to be read from the stream
- RD: The type of the ReadCloser resource (must implement io.ReadCloser)
Parameters:
- acquire: An IOEither that produces the ReadCloser resource
Returns:
A Kleisli function that takes a reader function (which transforms RD to R) and returns an IOEither that produces the read result R or an error.
Example:
import (
"os"
"io"
F "github.com/IBM/fp-go/v2/function"
"github.com/IBM/fp-go/v2/ioeither"
"github.com/IBM/fp-go/v2/ioeither/file"
)
// Read first 10 bytes from a file
readFirst10 := func(f *os.File) ioeither.IOEither[error, []byte] {
return ioeither.TryCatchError(func() ([]byte, error) {
buf := make([]byte, 10)
n, err := f.Read(buf)
return buf[:n], err
})
}
result := F.Pipe1(
file.Open("data.txt"),
file.Read[[]byte, *os.File],
)(readFirst10)
data, err := result()
if err != nil {
log.Fatal(err)
}
fmt.Printf("Read: %s\n", data)
The Read function ensures that the file is closed even if the reading operation fails, providing safe and composable resource management in a functional style.
Click to show internal directories.
Click to hide internal directories.