go-homedir to detect the home directory and expand paths.
Complete Example
Core Functions
Dir() - Get Home Directory
TheDir() function returns the home directory for the current user:
- Automatically caches the result for better performance
- Uses OS-specific detection methods
- Returns an error if the home directory cannot be determined
Expand() - Expand Tilde Paths
TheExpand() function expands ~ in paths to the full home directory:
- Only expands paths starting with
~followed by/or\ - Returns the path unchanged if it doesn’t start with
~ - Returns an error for user-specific home directories like
~otheruser/path - Handles empty paths gracefully
Advanced Usage
Disabling Cache
By default,go-homedir caches the home directory. You can disable this:
Resetting Cache
If you need to clear the cache (useful in tests):Practical Use Cases
Common Patterns
Building File Paths
Combinehomedir.Dir() with filepath.Join() for robust path construction:
User Input Processing
Expand user-provided paths that might contain~:
Next Steps
API Reference
Explore all available functions and options
Examples
See more real-world usage examples