Documentation
¶
Overview ¶
Package xdg implements the XDG Base Directory Specification in Go. The specification states where specification files should be searched. It does so by defining base directories relative to which files are located. The details of the specification are laid out here: https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
This package supports most Unix-based operating systems. It should work fine on MacOS.
Usage
package main import ( "fmt" "github.com/mdm-code/xdg" ) func main() { // XDG base directory paths. dirs := []struct { msg string f func() string }{ {"Home data directory: ", xdg.DataHomeDir}, {"Config home directory: ", xdg.CacheHomeDir}, {"State home directory: ", xdg.StateHomeDir}, {"Data directories: ", xdg.DataDirs}, {"Config directories: ", xdg.ConfigDirs}, {"Cache home directory: ", xdg.CacheHomeDir}, {"Runtime home directory: ", xdg.RuntimeDir}, } for _, d := range dirs { fmt.Println(d.msg, d.f()) } // Search for file in data XDG directories. fpath := "program/file.data" if f, ok := xdg.Find(xdg.Data, fpath); ok { fmt.Println(f) } else { fmt.Printf("ERROR: couldn't find %s\n", fpath) } }
Index ¶
Examples ¶
Constants ¶
const ( // Data XDG base directory type. Data dir = iota // Config XDG base directory type. Config // State XDG base directory type. State // Cache XDG base directory type. Cache // Runtime XDG base directory type. Runtime )
Variables ¶
This section is empty.
Functions ¶
func CacheHomeDir ¶
func CacheHomeDir() string
CacheHomeDir returns a single base directory relative to which user-specific, non-essential (cached) data should be written. Default: $HOME/.cache.
Example ¶
package main import ( "fmt" "github.com/mdm-code/xdg" ) func main() { dir := xdg.CacheHomeDir() fmt.Println("Cache home directory: ", dir) }
Output:
func ConfigDirs ¶
func ConfigDirs() string
ConfigDirs returns a set of preference-ordered base directories relative to which configuration files should be searched. Default: /etc/xdg.
Example ¶
package main import ( "fmt" "github.com/mdm-code/xdg" ) func main() { dir := xdg.ConfigDirs() fmt.Println("Config directories: ", dir) }
Output:
func ConfigHomeDir ¶
func ConfigHomeDir() string
ConfigHomeDir returns a single base directory relative to which user-specific configuration files should be written. Default: $HOME/.config.
Example ¶
package main import ( "fmt" "github.com/mdm-code/xdg" ) func main() { dir := xdg.ConfigHomeDir() fmt.Println("Config home directory: ", dir) }
Output:
func DataDirs ¶
func DataDirs() string
DataDirs returns a set of preference-ordered base directories relative to which data files should be searched. Default: /usr/local/share/:/usr/share/.
Example ¶
package main import ( "fmt" "github.com/mdm-code/xdg" ) func main() { dir := xdg.DataDirs() fmt.Println("Data directories: ", dir) }
Output:
func DataHomeDir ¶
func DataHomeDir() string
DataHomeDir returns a single base directory relative to which user-specific files should be written. Default: $HOME/.local/share.
Example ¶
package main import ( "fmt" "github.com/mdm-code/xdg" ) func main() { dir := xdg.DataHomeDir() fmt.Println("Home data directory: ", dir) }
Output:
func Find ¶
Find searches for a given path in specified XDG directories. It returns an empty string and false if the path is not found.
Example ¶
package main import ( "fmt" "github.com/mdm-code/xdg" ) func main() { fpath := "program/file.data" if f, ok := xdg.Find(xdg.Data, fpath); ok { fmt.Printf("Data file for %s was found at %s\n", fpath, f) } else { fmt.Printf("ERROR: couldn't find %s\n", fpath) } }
Output:
func RuntimeDir ¶
func RuntimeDir() string
RuntimeDir returns a single base directory relative to which user-specific, runtime files and other file objects should be placed. It defaults to $TMPDIR on Unix if non-empty else /tmp.
Example ¶
package main import ( "fmt" "github.com/mdm-code/xdg" ) func main() { dir := xdg.RuntimeDir() fmt.Println("Runtime home directory: ", dir) }
Output:
func StateHomeDir ¶
func StateHomeDir() string
StateHomeDir returns a single base directory relative to which user-specific state data should be written. Default: $HOME/.local/state.
Example ¶
package main import ( "fmt" "github.com/mdm-code/xdg" ) func main() { dir := xdg.StateHomeDir() fmt.Println("State home directory: ", dir) }
Output:
Types ¶
This section is empty.