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 ¶
- type File
- func (f File) Absolute() File
- func (f File) Base() string
- func (f *File) Chmod(mode fs.FileMode) error
- func (f File) Copies(copies ...File) error
- func (f File) Copy(dest File) error
- func (f File) Create() error
- func (f File) Dir() string
- func (f File) Exists() bool
- func (f File) Expanded() File
- func (f File) Extension() string
- func (f File) Hardlinks(hardlinks ...File) error
- func (f File) HasExtension() bool
- func (f File) Info() (fs.FileInfo, error)
- func (f File) IsDir() bool
- func (f File) IsExecutable() (bool, error)
- func (f File) IsFile() bool
- func (f File) Join(paths ...string) File
- func (f File) Links(links ...File) error
- func (f File) Matches(pattern string) (bool, error)
- func (f File) Open() (*os.File, error)
- func (f File) OpenForWriting() (*os.File, error)
- func (f File) Path() string
- func (f File) Read() ([]byte, error)
- func (f File) RelativeTo(base File) (File, error)
- func (f File) Remove() error
- func (f File) Softlinks(softlinks ...File) error
- func (f File) String() string
- func (f File) Unescape() File
- func (f File) WithExtension(extension string) File
- func (f File) WithoutExtension() File
- func (f File) Write(data []byte) (err error)
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
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 ¶
New creates a File from one or more path components. Joins the paths using filepath.Join and normalizes the result.
func (*File) Chmod ¶
Chmod modifies the file's permission bits. Takes a fs.FileMode parameter specifying the new permissions.
func (File) Copies ¶ added in v0.0.13
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 ¶
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 ¶
Create creates a new empty file at this path. Returns an error if the file cannot be created.
func (File) Dir ¶
Dir returns the containing directory path. Returns the path itself if it's a directory, otherwise returns the parent directory path.
func (File) Exists ¶
Exists checks if the path exists in the filesystem. Returns true if the path exists, false otherwise.
func (File) Expanded ¶
Expanded resolves home directory references. Replaces ~ with the user's home directory path.
func (File) Extension ¶
Extension returns the file's extension as a string, without the leading dot.
func (File) Hardlinks ¶ added in v0.0.13
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
HasExtension checks if the file has an extension.
func (File) IsDir ¶
IsDir checks if the path is a directory. Returns false for regular files and non-existent paths.
func (File) IsExecutable ¶
IsExecutable checks if the file has execute permissions. Returns true if any execute bit (user/group/other) is set.
func (File) IsFile ¶
IsFile checks if the path is a regular file. Returns false for directories, symlinks, and special files.
func (File) Join ¶
Join combines this path with additional components. Returns a new File with the combined path.
func (File) Links ¶ added in v0.0.13
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) Matches ¶ added in v0.0.13
Matches checks if the file path matches the given (extended glob) pattern.
func (File) Open ¶
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 ¶
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) Read ¶
Read retrieves the entire contents of the file. Returns the file contents as a byte slice.
func (File) RelativeTo ¶
RelativeTo computes the relative path from base to this file. Returns an error if the relative path cannot be computed.
func (File) Remove ¶
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) Softlinks ¶ added in v0.0.13
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) Unescape ¶
Unescape decodes URL-escaped characters in the path. Returns the original path if unescaping fails.
func (File) WithExtension ¶ added in v0.0.13
WithExtension returns a new File with the specified suffix added to the original file name.
func (File) WithoutExtension ¶
WithoutExtension returns the path without file extensions.