You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This library provides a cross platform means of detecting the system's
configuration directories so that your Go app can store config files in a
standard location. For Linux and other Unixes (BSD, etc.) this means using the
Freedesktop.org XDG Base Directory Specification (0.8), and for Windows
and macOS it uses their standard directories.
This is a simple no-nonsense module that just gives you the path names to do
with as you please. You can either get the bare root config path, or get a
path with any number of names suffixed onto it for vendor- or
application-specific namespacing.
For the impatient, the directories this library can return tend to be like
the following:
System-wide Configuration
Windows
%PROGRAMDATA% or C:\ProgramData
Linux
$XDG_CONFIG_DIRS or /etc/xdg
macOS
/Library/Application Support
User-level Configuration
Windows
%APPDATA% or C:\Users\%USER%\AppData\Roaming
Linux
$XDG_CONFIG_HOME or $HOME/.config
macOS
$HOME/Library/Application Support
User-level Cache Folder
Windows
%LOCALAPPDATA% or C:\Users\%USER%\AppData\Local
Linux
$XDG_CACHE_HOME or $HOME/.cache
macOS
$HOME/Library/Caches
Quick Start
// A common use case is to get a private config folder for your app to// place its settings files into, that are specific to the local user.configPath:=configdir.LocalConfig("my-app")
err:=configdir.MakePath(configPath) // Ensure it exists.iferr!=nil {
panic(err)
}
// Deal with a JSON configuration file in that folder.configFile:=filepath.Join(configPath, "settings.json")
typeAppSettingsstruct {
Usernamestring`json:"username"`Passwordstring`json:"password"`
}
varsettingsAppSettings// Does the file not exist?if_, err=os.Stat(configFile); os.IsNotExist(err) {
// Create the new config file.settings=AppSettings{"MyUser", "MyPassword"}
fh, err:=os.Create(configFile)
iferr!=nil {
panic(err)
}
deferfh.Close()
encoder:=json.NewEncoder(fh)
encoder.Encode(&settings)
} else {
// Load the existing file.fh, err:=os.Open(configFile)
iferr!=nil {
panic(err)
}
deferfh.Close()
decoder:=json.NewDecoder(fh)
decoder.Decode(&settings)
}