file

package
v0.1.12 Latest Latest
Warning

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

Go to latest
Published: Feb 12, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package file provides a typed abstraction over filesystem file paths. It offers utilities for file manipulation, path operations, and content I/O.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type File

type File string

File represents a filesystem path with associated operations. Provides methods for file manipulation, path operations, and common file system tasks.

func CreateRandomInDir added in v0.0.13

func CreateRandomInDir(dir, pattern string) (File, error)

CreateRandomInDir creates a uniquely named file. Creates a file with a random name inside the specified directory. Use empty string for directory to create in system temp directory. Pattern is used as a prefix for the random file name.

func New

func New(paths ...string) File

New creates a File from one or more path components. Joins the paths using filepath.Join and normalizes the result.

func (File) Absolute

func (f File) Absolute() File

Absolute returns the absolute path of the file.

func (File) Base

func (f File) Base() string

Base returns the last component of the file path.

func (*File) Chmod

func (f *File) Chmod(mode fs.FileMode) error

Chmod modifies the file's permission bits. Takes a fs.FileMode parameter specifying the new permissions.

func (File) Copies added in v0.0.13

func (f File) Copies(copies ...File) error

Copies duplicates the file to a new location. It copies contents and preserves the original file's permissions. Timestamps and ownership are not preserved for cross-platform compatibility.

func (File) Copy

func (f File) Copy(dest File) error

Copy duplicates the file to a new location. It copies contents and preserves the original file's permissions. Timestamps and ownership are not preserved for cross-platform compatibility.

func (File) Create

func (f File) Create() error

Create creates a new empty file at this path. Returns an error if the file cannot be created.

func (File) Dir

func (f File) Dir() string

Dir returns the containing directory path. Returns the path itself if it's a directory, otherwise returns the parent directory path.

func (File) Exists

func (f File) Exists() bool

Exists checks if the path exists in the filesystem. Returns true if the path exists, false otherwise.

func (File) Expanded

func (f File) Expanded() File

Expanded resolves environment variables including `~` home directory references.

func (File) Extension

func (f File) Extension() string

Extension returns the file's extension as a string, without the leading dot.

func (f File) Hardlinks(hardlinks ...File) error

Hardlinks creates a hard link at the destination path pointing to this file. Returns an error if the operation is not permitted, the files are on different devices, or the source file is not a regular file.

func (File) HasExtension added in v0.0.13

func (f File) HasExtension() bool

HasExtension checks if the file has an extension.

func (File) Hash added in v0.1.0

func (f File) Hash() (string, error)

Hash computes the hash of the file's contents.

func (File) InPath added in v0.1.0

func (f File) InPath() bool

InPath checks if the file can be found in the system PATH. Returns true if the file is found in PATH, false otherwise.

func (File) Info added in v0.0.13

func (f File) Info() (fs.FileInfo, error)

Info retrieves the file information.

func (File) IsBinaryLike added in v0.1.0

func (f File) IsBinaryLike() bool

IsBinaryLike checks if the file is binary-like.

func (File) IsDir

func (f File) IsDir() bool

IsDir checks if the path is a directory. Returns false for regular files and non-existent paths.

func (File) IsExecutable

func (f File) IsExecutable() (bool, error)

IsExecutable checks if the file has execute permissions. Returns true if any execute bit (user/group/other) is set.

func (File) IsFile

func (f File) IsFile() bool

IsFile checks if the path is a regular file. Returns false for directories, symlinks, and special files.

func (File) Join

func (f File) Join(paths ...string) File

Join combines this path with additional components. Returns a new File with the combined path.

func (File) LargerThan added in v0.1.0

func (f File) LargerThan(size int64) (bool, error)

LargerThan checks if the file is larger than the specified size in bytes.

func (File) Lines added in v0.1.0

func (f File) Lines() ([]string, error)

Lines retrieves the file contents as a slice of strings.

func (f File) Links(links ...File) error

Links tries to create symbolic links to this file at the given targets. If symlinking fails (e.g., unsupported platform, permissions, or FS restrictions), it attempts a hard link. If that also fails, it falls back to copying the file.

This function is a convenience helper for creating portable references to a file without requiring the caller to explicitly handle platform quirks.

func (File) MakeExecutable added in v0.0.18

func (f File) MakeExecutable() error

MakeExecutable sets the file as executable for user, group, and others.

func (File) Matches added in v0.0.13

func (f File) Matches(pattern string) (bool, error)

Matches checks if the file path matches the given (extended glob) pattern.

func (File) NumberOfLines added in v0.1.0

func (f File) NumberOfLines() (int, error)

NumberOfLines returns the number of lines in the file.

func (File) Open

func (f File) Open() (*os.File, error)

Open opens the file for reading and returns a pointer to the os.File object. The user must close the file after use.

func (File) OpenForWriting

func (f File) OpenForWriting() (*os.File, error)

OpenForWriting opens the file for writing and returns a pointer to the os.File object. If the file doesn't exist, it will be created. If it exists, it will be truncated. The user must close the file after use.

func (File) Path

func (f File) Path() string

Path returns the string representation of the file path.

func (File) Read

func (f File) Read() ([]byte, error)

Read retrieves the entire contents of the file. Returns the file contents as a byte slice.

func (File) ReadString added in v0.1.11

func (f File) ReadString() (string, error)

ReadString retrieves the entire contents of the file as a string.

func (File) RelativeTo

func (f File) RelativeTo(base string) (File, error)

RelativeTo computes the relative path from base to this file. Returns an error if the relative path cannot be computed.

func (File) Remove

func (f File) Remove() error

Remove deletes the file from the filesystem. Returns an error if the file cannot be deleted. Silently ignores the error if the file does not exist.

func (File) Set added in v0.1.0

func (f File) Set() bool

Set checks if the path is non-empty.

func (File) Size added in v0.1.0

func (f File) Size() (int64, error)

Size returns the size of the file in bytes.

func (File) SmallerThan added in v0.1.0

func (f File) SmallerThan(size int64) (bool, error)

SmallerThan checks if the file is smaller than the specified size in bytes.

func (f File) Softlinks(softlinks ...File) error

Softlinks creates symbolic links to this file on Unix-like systems. Takes multiple target paths and creates a symlink at each location. Skips existing symlinks without error, but returns an error if symlink creation fails for any other reason. Not available on Windows.

func (File) String

func (f File) String() string

String returns the file path as a string.

func (File) Unescape

func (f File) Unescape() File

Unescape decodes URL-escaped characters in the path. Returns the original path if unescaping fails.

func (File) Up added in v0.1.4

func (f File) Up() File

Up returns the parent directory of the file path.

func (File) WithExtension added in v0.0.13

func (f File) WithExtension(extension string) File

WithExtension returns a new File with the specified suffix added to the original file name.

func (File) WithoutExtension

func (f File) WithoutExtension() File

WithoutExtension returns the path without file extensions.

func (File) WithoutExtensions added in v0.1.10

func (f File) WithoutExtensions() File

WithoutExtensions keeps calling WithoutExtension until no dots remain in the base name.

func (File) WithoutFolder added in v0.1.0

func (f File) WithoutFolder(prefix string) File

WithoutFolder strips the leading folder path from f if it matches as a full path prefix (on a segment boundary). It never leaves a leading "/" behind.

func (File) Write

func (f File) Write(data []byte) (err error)

Write stores binary data in the file. Creates or truncates the file before writing.

Jump to

Keyboard shortcuts

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