go-fileutils
A comprehensive file utilities library for Go that provides a set of convenient and robust file system operations.
Features
- File Operations: Read, write, and copy files with proper error handling
- Directory Operations: Create, remove, and copy directories recursively
- Path Operations: Extract basename, dirname, extname, and work with absolute paths
- Symlink Support: Create and manage symbolic links
- Path Utilities: Join paths, check file existence, and validate path types
- Error Handling: Comprehensive error messages for all operations
Installation
go get github.com/afeiship/go-fileutils
Usage
File Operations
package main
import (
"fmt"
"github.com/afeiship/go-fileutils"
)
func main() {
// Read file contents
data, err := fileutils.ReadFile("example.txt")
if err != nil {
fmt.Printf("Error reading file: %v\n", err)
return
}
fmt.Printf("File content: %s\n", string(data))
// Write file contents (creates parent directories if needed)
err = fileutils.WriteFile("output/example.txt", []byte("Hello, World!"))
if err != nil {
fmt.Printf("Error writing file: %v\n", err)
return
}
// Quick read/write operations (panic on error)
content := fileutils.ReadContents("example.txt")
fileutils.WriteContents("output/quick.txt", "Quick content")
}
Directory Operations
// Create directory (and parent directories if needed)
err := fileutils.Mkdir("data/subdir")
// Remove directory or file recursively
err = fileutils.RmRf("data")
// Copy directory recursively
err = fileutils.CopyDir("src", "dst")
// Change working directory
err = fileutils.Chdir("/tmp")
File System Checks
// Check if path exists
if fileutils.IsExists("example.txt") {
fmt.Println("File exists")
}
// Check if path is a file
if fileutils.IsFile("example.txt") {
fmt.Println("This is a file")
}
// Check if path is a directory
if fileutils.IsDir("data") {
fmt.Println("This is a directory")
}
// Check if path is a symbolic link
if fileutils.IsLink("symlink") {
fmt.Println("This is a symbolic link")
}
Path Operations
// Get base name
base := fileutils.Basename("/path/to/file.txt") // "file.txt"
// Get directory name
dir := fileutils.Dirname("/path/to/file.txt") // "/path/to"
// Get file extension
ext := fileutils.Extname("/path/to/file.txt") // ".txt"
// Get absolute path
abs, err := fileutils.Absname("relative/path")
// Get home directory
home, err := fileutils.HomeDir()
// Join path elements (filters empty strings)
path := fileutils.Join("path", "to", "file.txt") // "path/to/file.txt"
Copy Operations
// Copy a single file
err := fileutils.CopyFile("source.txt", "destination.txt")
// Copy directory recursively (preserves permissions and symlinks)
err = fileutils.CopyDir("source_dir", "destination_dir")
Symbolic Links
// Create symbolic link
err := fileutils.Ln("/path/to/target", "/path/to/link")
// Check if path is symbolic link
isLink := fileutils.IsLink("/path/to/link")
Rename Operations
// Rename/move file or directory
err := fileutils.Rename("oldname.txt", "newname.txt")
API Reference
File Functions
ReadFile(filename string) ([]byte, error) - Read file with error handling
WriteFile(filename string, data []byte) error - Write file with error handling
ReadContents(filename string) string - Read file contents (panics on error)
WriteContents(filename string, data string) - Write file contents (panics on error)
Directory Functions
Mkdir(path string) error - Create directory with parents
RmRf(path string) error - Remove path recursively
Chdir(path string) error - Change working directory
CopyDir(src string, dst string) error - Copy directory recursively
Path Functions
Basename(path string) string - Get base name
Dirname(path string) string - Get directory name
Extname(path string) string - Get file extension
Absname(path string) (string, error) - Get absolute path
HomeDir() (string, error) - Get home directory
Join(elem ...string) string - Join path elements
Check Functions
IsExists(path string) bool - Check if path exists
IsFile(path string) bool - Check if path is a file
IsDir(path string) bool - Check if path is a directory
IsLink(path string) bool - Check if path is a symbolic link
Utility Functions
CopyFile(srcFile string, destFile string) error - Copy single file
Rename(oldpath, newpath string) error - Rename/move file or directory
Ln(src string, dst string) error - Create symbolic link
Error Handling
All functions that return errors provide descriptive error messages to help with debugging. The library includes comprehensive error constants for different error scenarios.
Testing
Run the tests:
go test ./...
License
MIT License
Contributing
Pull requests are welcome! Please feel free to submit a Pull Request.
评论