repositorypackage
0.1.1
Repository: https://github.com/karagenc/finddirs-go.git
Documentation: pkg.go.dev
# README
finddirs
This is a Go package for retrieving common directories found across all operating systems.
Locations
Application Directories
Directory | Unix [1][2] | Windows [3] | macOS & iOS [5] | Plan 9 |
---|---|---|---|---|
Config directory (system-wide) | /etc | C:/ProgramData | /Library/Application Support | /lib |
Config directory (local) | ~/.config | C:/<user>/AppData/<Local or Roaming> [4] | ~/Library/Application Support | ~/lib |
State directory (system-wide) | /var/lib | C:/ProgramData | /Library/Application Support | /lib |
State directory (local) | ~/.local/state | C:/<user>/AppData/Local | ~/Library/Application Support | ~/lib |
Cache directory (system-wide) | /var/cache | C:/ProgramData | /Library/Caches | /lib/cache |
Cache directory (local) | ~/.cache | C:/<user>/AppData/Local | ~/Library/Caches | ~/lib/cache |
- On Unix based systems, XDG environment variables
$XDG_CONFIG_HOME
,$XDG_STATE_HOME
, and$XDG_CACHE_HOME
are first tried for paths~/.config
,~/.local/state
, and~/.cache
respectively. If the particular XDG environment variable is set, it is used instead. - If Termux is detected on Android, system-wide directories will be under
~/../usr
(of course, as an absolute path). - On Windows, KNOWNFOLDERID constants are used.
- Usage of
AppData\Local
orAppData\Roaming
depends on whetherUseRoaming
is set to true inConfig
struct. - System-wide directories are not supported on iOS — iOS apps are inside a sandbox, therefore system-wide directories cannot be accessed. Calling
RetrieveAppDirs
withsystemWide
argument set to true will result with an error.
User Directories
Directory | Unix [1], macOS, and Windows (Also See [2], [3], and [4]) |
---|---|
Desktop | ~/Desktop |
Downloads | ~/Downloads |
Documents | ~/Documents |
Pictures | ~/Pictures |
Videos | ~/Videos on Linux and Windows, ~/Movies on macOS |
Music | ~/Music |
Templates | ~/Templates |
Fonts | $XDG_DATA_HOME/fonts , ~/.local/share/fonts , ~/.fonts , /usr/share/fonts , and /usr/local/share/fonts on Linux |
~/Library/Fonts , /Library/Fonts , /System/Library/Fonts , and /Network/Library/Fonts on macOS | |
C:/WINDOWS/Fonts and C:/Users/<USER>/AppData/Local/Microsoft/Windows/Fonts on Windows | |
PublicShare | ~/Public on Linux and macOS, C:\Users\Public on Windows |
- On Unix based systems, entries in
user-dirs.dirs
are read. Ifuser-dirs.dirs
cannot be found, or it's malformed,RetrieveUserDirs
returns with error. If an entry is$HOME/
(that means, it is empty), it is set to an empty string (""
), and no error is returned. On Unix, check for empty directories. - Plan 9 is not supported.
RetrieveUserDirs
on a Plan 9 system will return an error. - If Termux is detected on Android, the Desktop, Templates, Fonts, and PublicShare directories will be empty, as they don't exist on the that platform.
- iOS is not supported.
RetrieveUserDirs
on an iOS system will return an error.
Usage
Very straightforward: go get -u github.com/karagenc/finddirs-go
package main
import (
"fmt"
"github.com/karagenc/finddirs-go"
)
func main() {
userAppDirs, _ := finddirs.RetrieveAppDirs(false, nil)
fmt.Println(userAppDirs)
systemAppDirs, _ := finddirs.RetrieveAppDirs(true, nil)
fmt.Println(systemAppDirs)
userDirs, _ := finddirs.RetrieveUserDirs()
fmt.Println(userDirs)
}
Remarks/Notes
-
Since you're dealing with directories:
- Let me recommend github.com/mitchellh/go-homedir. It is used by this library and is much more reliable than
os.UserHomeDir()
. - For those who want to dive deep, here are platform-specific documentations:
- Let me recommend github.com/mitchellh/go-homedir. It is used by this library and is much more reliable than
-
filepath.ToSlash
was used for every directory returned from functions in this package. This is to prevent shellwords from interpreting backslash as escape character. To test this behavior, remove this line, and see what happens. Usefilepath.FromSlash
to convert slashes to OS-specific path seperators.