Documentation
¶
Overview ¶
Package stream provides utility functions for handling file I/O operations. This package simplifies reading, writing, appending, copying files, and more.
Index ¶
- func AppendToFile(path, data string) error
- func CopyFile(src, dst string) error
- func Exists(path string) bool
- func ListFiles(dir string, recursive bool) ([]string, error)
- func ReadDirFiles(dirPath string, ext string) (map[string]string, error)
- func ReadFile(path string) (string, error)
- func WriteFile(path, data string) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AppendToFile ¶
AppendToFile appends data to an existing file. If the file does not exist, it will create it. Proper error handling is done for the deferred file close operation.
Parameters: - path: the path of the file to append to - data: the data to append to the file
Returns: - error: if an error occurs
func CopyFile ¶
CopyFile copies a file from src to dst. If the destination file exists, it will be overwritten. This uses modern I/O methods to ensure optimal performance and error handling.
Parameters: - src: the source file to copy - dst: the destination file
Returns: - error: if an error occurs
func Exists ¶
Exists checks whether a file or directory exists at the given path. It uses os.Stat, which is efficient and widely used for checking file existence.
Parameters: - path: the path to check
Returns: - bool: true if the file or directory exists, false otherwise
func ListFiles ¶
ListFiles returns a list of all files (not directories) in the given directory. If the recursive flag is true, it will search through subdirectories as well.
Parameters: - dir: the directory to list files from - recursive: whether to search through subdirectories
Returns: - []string: a list of file paths - error: if an error occurs
func ReadDirFiles ¶
ReadDirFiles reads the contents of all files in a directory and returns them as a map with file names as keys and file content as values. It supports filtering files by extension.
Parameters: - dirPath: the path of the directory to read - ext: the file extension to filter by
Returns: - map[string]string: a map of file names to file contents - error: if an error occurs
func ReadFile ¶
ReadFile reads the content of a file and returns it as a string. It uses os.ReadFile to handle the file reading, which is the modern and recommended approach.
Parameters: - path: the path of the file to read
Returns: - string: the content of the file - error: if an error occurs
func WriteFile ¶
WriteFile writes data to a file. It creates the file if it doesn't exist and overwrites it if it does. This uses os.WriteFile, which is a modern API for file writing.
Parameters: - path: the path of the file to write to - data: the data to write to the file
Returns: - error: if an error occurs
Types ¶
This section is empty.