A comprehensive file utilities library for Go that provides a set of convenient and robust file system operations.
- 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
go get github.com/afeiship/go-fileutilspackage 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")
}// 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")// 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")
}// 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 a single file
err := fileutils.CopyFile("source.txt", "destination.txt")
// Copy directory recursively (preserves permissions and symlinks)
err = fileutils.CopyDir("source_dir", "destination_dir")// 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/move file or directory
err := fileutils.Rename("oldname.txt", "newname.txt")ReadFile(filename string) ([]byte, error)- Read file with error handlingWriteFile(filename string, data []byte) error- Write file with error handlingReadContents(filename string) string- Read file contents (panics on error)WriteContents(filename string, data string)- Write file contents (panics on error)
Mkdir(path string) error- Create directory with parentsRmRf(path string) error- Remove path recursivelyChdir(path string) error- Change working directoryCopyDir(src string, dst string) error- Copy directory recursively
Basename(path string) string- Get base nameDirname(path string) string- Get directory nameExtname(path string) string- Get file extensionAbsname(path string) (string, error)- Get absolute pathHomeDir() (string, error)- Get home directoryJoin(elem ...string) string- Join path elements
IsExists(path string) bool- Check if path existsIsFile(path string) bool- Check if path is a fileIsDir(path string) bool- Check if path is a directoryIsLink(path string) bool- Check if path is a symbolic link
CopyFile(srcFile string, destFile string) error- Copy single fileRename(oldpath, newpath string) error- Rename/move file or directoryLn(src string, dst string) error- Create symbolic link
All functions that return errors provide descriptive error messages to help with debugging. The library includes comprehensive error constants for different error scenarios.
Run the tests:
go test ./...MIT License
Pull requests are welcome! Please feel free to submit a Pull Request.