stream

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2024 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package stream provides utility functions for handling file I/O operations. This package simplifies reading, writing, appending, copying files, and more.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AppendToFile

func AppendToFile(path, data string) error

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

func CopyFile(src, dst string) error

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

func Exists(path string) bool

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

func ListFiles(dir string, recursive bool) ([]string, error)

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

func ReadDirFiles(dirPath string, ext string) (map[string]string, error)

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

func ReadFile(path string) (string, error)

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

func WriteFile(path, data string) error

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.

Jump to

Keyboard shortcuts

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