Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork31
Go implementation of the XDG Base Directory Specification and XDG user directories
License
adrg/xdg
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Provides an implementation of theXDG Base Directory Specification.The specification defines a set of standard paths for storing application files,including data and configuration files. For portability and flexibility reasons,applications should use the XDG defined locations instead of hardcoding paths.
The package also includes the locations of well knownuser directories,support for the non-standardXDG_BIN_HOME
directory, as well as other common directories such as fonts and applications.
The current implementation supportsmost flavors of Unix,Windows,macOS andPlan 9.
On Windows, where XDG environment variables are not usually set, the package usesKnown Foldersas defaults. Therefore, appropriate locations are used for commonfolders which may have been redirected.
See usageexamples below. Full documentation can be found athttps://pkg.go.dev/github.com/adrg/xdg.
go get github.com/adrg/xdg
The package defines sensible defaults for XDG variables which are empty or notpresent in the environment.
- On Unix-like operating systems, XDG environment variables are typically defined.Appropriate default locations are used for the environment variables which are not set.
- On Windows, XDG environment variables are usually not set. If that is the case,the package relies on the appropriateKnown Folders.Sensible fallback locations are used for the folders which are not set.
Unix-like operating systems
Microsoft Windows
XDG user directories environment variables are usuallynot set on mostoperating systems. However, if they are present in the environment, they takeprecedence. Appropriate fallback locations are used for the environmentvariables which are not set.
- On Unix-like operating systems (except macOS and Plan 9), the package reads theuser-dirs.dirs config file.
- On Windows, the package uses the appropriateKnown Folders.
Lastly, default locations are used for any user directories which are not set,as shown in the following tables.
Unix-like operating systems
Microsoft Windows
Unix-like operating systems
Microsoft Windows
package mainimport ("log""github.com/adrg/xdg")funcmain() {// XDG Base Directory paths.log.Println("Home data directory:",xdg.DataHome)log.Println("Data directories:",xdg.DataDirs)log.Println("Home config directory:",xdg.ConfigHome)log.Println("Config directories:",xdg.ConfigDirs)log.Println("Home state directory:",xdg.StateHome)log.Println("Cache directory:",xdg.CacheHome)log.Println("Runtime directory:",xdg.RuntimeDir)log.Println("Home binaries directory:",xdg.BinHome)// Other common directories.log.Println("Home directory:",xdg.Home)log.Println("Application directories:",xdg.ApplicationDirs)log.Println("Font directories:",xdg.FontDirs)// Obtain a suitable location for application config files.// ConfigFile takes one parameter which must contain the name of the file,// but it can also contain a set of parent directories. If the directories// don't exist, they will be created relative to the base config directory.// It is recommended for files to be saved inside an application directory// relative to the base directory rather than directly inside the base// directory (e.g. `appname/config.yaml` instead of `appname-config.yaml`).configFilePath,err:=xdg.ConfigFile("appname/config.yaml")iferr!=nil {log.Fatal(err)}log.Println("Save the config file at:",configFilePath)// For other types of application files use:// xdg.DataFile()// xdg.StateFile()// xdg.CacheFile()// xdg.RuntimeFile()// Finding application config files.// SearchConfigFile takes one parameter which must contain the name of// the file, but it can also contain a set of parent directories relative// to the config search paths (xdg.ConfigHome and xdg.ConfigDirs).configFilePath,err=xdg.SearchConfigFile("appname/config.yaml")iferr!=nil {log.Fatal(err)}log.Println("Config file was found at:",configFilePath)// For other types of application files use:// xdg.SearchDataFile()// xdg.SearchStateFile()// xdg.SearchCacheFile()// xdg.SearchRuntimeFile()}
package mainimport ("log""github.com/adrg/xdg")funcmain() {// XDG user directories.log.Println("Desktop directory:",xdg.UserDirs.Desktop)log.Println("Download directory:",xdg.UserDirs.Download)log.Println("Documents directory:",xdg.UserDirs.Documents)log.Println("Music directory:",xdg.UserDirs.Music)log.Println("Pictures directory:",xdg.UserDirs.Pictures)log.Println("Videos directory:",xdg.UserDirs.Videos)log.Println("Templates directory:",xdg.UserDirs.Templates)log.Println("Public directory:",xdg.UserDirs.PublicShare)}
Contributions in the form of pull requests, issues or just general feedback,are always welcome.
SeeCONTRIBUTING.MD.
Contributors:adrg,wichert,bouncepaw,gabriel-vasile,KalleDK,nvkv,djdv,rrjjvv,GreyXor,Rican7,nothub,korikhin.
For more information see:
Copyright (c) 2014 Adrian-George Bostan.
This project is licensed under theMIT license.SeeLICENSE for more details.
About
Go implementation of the XDG Base Directory Specification and XDG user directories
Topics
Resources
License
Code of conduct
Uh oh!
There was an error while loading.Please reload this page.