Documentation
¶
Overview ¶
Package fileutil provides file system operations with proper error handling.
All functions in this package follow Go best practices: - Return errors instead of panicking - Accept context.Context for cancellable operations - Close resources properly using defer
Reading Files ¶
Functions for reading file contents:
lines, err := fileutil.ReadLines(ctx, "file.txt") content, err := fileutil.ReadString(ctx, "file.txt") data, err := fileutil.ReadBytes(ctx, "file.txt")
File Information ¶
Functions for checking file properties:
if fileutil.Exists(path) { ... }
if fileutil.IsDir(path) { ... }
size, err := fileutil.Size(path)
modTime, err := fileutil.ModTime(path)
Directory Operations ¶
Functions for working with directories:
files, err := fileutil.List(ctx, dir, fileutil.Recursive) files, err := fileutil.Find(ctx, dir, "*.go") err := fileutil.EnsureDir(path, 0755)
Line Terminator Handling ¶
Functions for handling different line endings:
terminator := fileutil.DetectLineTerminator(data) normalized := fileutil.NormalizeLineTerminators(data, fileutil.LF)
Context Support ¶
Long-running operations accept context.Context for cancellation:
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() lines, err := fileutil.ReadLines(ctx, "large-file.txt")
Index ¶
- Variables
- func Abs(path string) string
- func AppendBytes(path string, data []byte, perm fs.FileMode) error
- func AppendLine(path, line string, terminator LineTerminator, perm fs.FileMode) error
- func AppendString(path, content string, perm fs.FileMode) error
- func Base(path string) string
- func BaseName(path string) string
- func Clean(path string) string
- func Copy(ctx context.Context, src, dst string) error
- func CountLines(ctx context.Context, path string) (int, error)
- func Dir(path string) string
- func DirSize(ctx context.Context, dir string) (int64, error)
- func EnsureDir(path string, perm fs.FileMode) error
- func Exists(path string) bool
- func Extension(path string) string
- func Find(ctx context.Context, dir, pattern string) ([]string, error)
- func FindByExtension(ctx context.Context, dir, ext string) ([]string, error)
- func IsDir(path string) bool
- func IsEmpty(path string) (bool, error)
- func IsExecutable(path string) bool
- func IsFile(path string) bool
- func IsSymlink(path string) bool
- func Join(elem ...string) string
- func List(ctx context.Context, dir string, opts ListOption) ([]string, error)
- func ModTime(path string) (time.Time, error)
- func Mode(path string) (fs.FileMode, error)
- func Move(ctx context.Context, src, dst string) error
- func NormalizeLineTerminators(data []byte, target LineTerminator) []byte
- func ReadBytes(ctx context.Context, path string) ([]byte, error)
- func ReadLines(ctx context.Context, path string) ([]string, error)
- func ReadLinesN(ctx context.Context, path string, n int) ([]string, error)
- func ReadString(ctx context.Context, path string) (string, error)
- func Rel(base, target string) (string, error)
- func SameFile(path1, path2 string) bool
- func Size(path string) (int64, error)
- func Split(path string) (dir, file string)
- func TempDir(dir, pattern string) (string, error)
- func TempFile(dir, pattern string) (string, error)
- func Touch(path string) error
- func WriteBytes(path string, data []byte, perm fs.FileMode) error
- func WriteLines(path string, lines []string, terminator LineTerminator, perm fs.FileMode) error
- func WriteString(path, content string, perm fs.FileMode) error
- type LineTerminator
- type ListOption
Constants ¶
This section is empty.
Variables ¶
var ( ErrNotFile = errors.New("path is not a file") ErrNotDir = errors.New("path is not a directory") ErrNotExist = errors.New("path does not exist") ErrReadCanceled = errors.New("read operation canceled") )
Common errors.
Functions ¶
func Abs ¶
Abs returns the absolute path. Returns the path unchanged if it's already absolute or on error.
func AppendBytes ¶
AppendBytes appends data to a file, creating it if necessary.
func AppendLine ¶
func AppendLine(path, line string, terminator LineTerminator, perm fs.FileMode) error
AppendLine appends a line to a file with the specified terminator.
func AppendString ¶
AppendString appends a string to a file.
func CountLines ¶
CountLines counts the number of lines in a file. More memory-efficient than ReadLines for large files.
func EnsureDir ¶
EnsureDir creates a directory if it doesn't exist. Creates parent directories as needed (like mkdir -p).
func Extension ¶
Extension returns the file extension including the dot. Returns empty string if no extension.
func FindByExtension ¶
FindByExtension returns files with the specified extension. Extension should include the dot (e.g., ".go").
func IsEmpty ¶
IsEmpty reports whether the file or directory is empty. For files, returns true if size is 0. For directories, returns true if no entries exist.
func IsExecutable ¶
IsExecutable reports whether the path is executable.
func NormalizeLineTerminators ¶
func NormalizeLineTerminators(data []byte, target LineTerminator) []byte
NormalizeLineTerminators converts all line terminators to the specified style.
func ReadBytes ¶
ReadBytes reads the entire file and returns its contents as bytes. Respects context cancellation before starting the read.
func ReadLines ¶
ReadLines reads the file and returns its contents as a slice of lines. Line terminators are stripped from each line.
func ReadLinesN ¶
ReadLinesN reads the first n lines from a file. If n <= 0, reads all lines.
func ReadString ¶
ReadString reads the entire file and returns its contents as a string.
func TempDir ¶
TempDir creates a temporary directory and returns its path. The caller is responsible for removing the directory.
func TempFile ¶
TempFile creates a temporary file and returns its path. The caller is responsible for removing the file.
func WriteBytes ¶
WriteBytes writes data to a file, creating it if necessary. Truncates existing files.
func WriteLines ¶
WriteLines writes lines to a file, joining with the specified terminator.
Types ¶
type LineTerminator ¶
type LineTerminator int
LineTerminator represents different line ending styles.
const ( // LF represents Unix-style line endings (\n). LF LineTerminator = iota // CRLF represents Windows-style line endings (\r\n). CRLF // CR represents old Mac-style line endings (\r). CR // Mixed indicates the file has inconsistent line endings. Mixed // Unknown indicates no line endings were found. Unknown )
func DetectFileLineTerminator ¶
func DetectFileLineTerminator(ctx context.Context, path string) (LineTerminator, error)
DetectFileLineTerminator detects line terminator style in a file. Reads only the beginning of the file for efficiency.
func DetectLineTerminator ¶
func DetectLineTerminator(data []byte) LineTerminator
DetectLineTerminator detects the line terminator style in data. Returns Unknown if no line terminators are found. Returns Mixed if multiple styles are detected.
func (LineTerminator) Bytes ¶
func (lt LineTerminator) Bytes() []byte
Bytes returns the byte sequence for the line terminator.
func (LineTerminator) String ¶
func (lt LineTerminator) String() string
String returns the string representation of the line terminator.
type ListOption ¶
type ListOption int
ListOption configures List behavior.
const ( // FilesOnly lists only files (not directories). FilesOnly ListOption = 1 << iota // DirsOnly lists only directories (not files). DirsOnly // Recursive lists contents recursively. Recursive // IncludeHidden includes hidden files (starting with .) IncludeHidden )