xdg

package
v0.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 4, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package xdg provides XDG Base Directory Specification compliant paths.

This package abstracts platform-specific conventions for storing application files, following the XDG Base Directory Specification on Linux/Unix and using native conventions on macOS and Windows.

Platform Behavior

On Linux/Unix, it follows the XDG Base Directory Specification:

  • Config: $XDG_CONFIG_HOME or ~/.config
  • Data: $XDG_DATA_HOME or ~/.local/share
  • Cache: $XDG_CACHE_HOME or ~/.cache
  • State: $XDG_STATE_HOME or ~/.local/state

On macOS, it uses Apple conventions:

  • Config: ~/Library/Application Support
  • Data: ~/Library/Application Support
  • Cache: ~/Library/Caches
  • State: ~/Library/Application Support

On Windows, it uses standard Windows paths:

  • Config: %AppData%
  • Data: %AppData%
  • Cache: %LocalAppData%
  • State: %AppData%

Usage

The package uses a single source of truth pattern for the application name. Set the app name once at startup:

func init() {
    xdg.SetAppName("myapp")
}

Then use the package functions throughout your application:

// Get config directory (creates if needed)
configDir, err := xdg.ConfigDir()
// Returns: ~/.config/myapp on Linux

// Get path to a config file
configPath, err := xdg.ConfigFile("config.yaml")
// Returns: ~/.config/myapp/config.yaml on Linux

// Get cache directory for temporary/regenerable data
cacheDir, err := xdg.CacheDir()
// Returns: ~/.cache/myapp on Linux

// Get state directory for logs, history, etc.
stateDir, err := xdg.StateDir()
// Returns: ~/.local/state/myapp on Linux

Directory Types

Choose the appropriate directory type based on your data:

  • Config: User configuration files (settings, preferences)
  • Data: Persistent application data (databases, user files)
  • Cache: Regenerable data (can be deleted without data loss)
  • State: Runtime state (logs, history, recently-used)

Thread Safety

All functions in this package are safe for concurrent use. SetAppName should ideally be called once during initialization.

Package xdg provides XDG Base Directory Specification compliant paths.

On Linux/Unix, it follows the XDG spec:

  • Config: $XDG_CONFIG_HOME or ~/.config
  • Data: $XDG_DATA_HOME or ~/.local/share
  • Cache: $XDG_CACHE_HOME or ~/.cache
  • State: $XDG_STATE_HOME or ~/.local/state

On macOS, it uses Apple conventions:

  • Config: ~/Library/Application Support
  • Data: ~/Library/Application Support
  • Cache: ~/Library/Caches
  • State: ~/Library/Application Support

On Windows, it uses standard Windows paths:

  • Config: %AppData%
  • Data: %AppData%
  • Cache: %LocalAppData%
  • State: %AppData%

Usage:

// At startup, set the app name from your single source of truth
xdg.SetAppName(binaryName)

// Then use the package functions
configDir, _ := xdg.ConfigDir()
cacheFile, _ := xdg.CacheFile("timings.json")

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CacheDir

func CacheDir() (string, error)

CacheDir returns the directory for cache files. Creates the directory if it doesn't exist.

func CacheFile

func CacheFile(filename string) (string, error)

CacheFile returns the path to a cache file.

func ConfigDir

func ConfigDir() (string, error)

ConfigDir returns the directory for configuration files. Creates the directory if it doesn't exist.

func ConfigFile

func ConfigFile(filename string) (string, error)

ConfigFile returns the path to a config file.

func DataDir

func DataDir() (string, error)

DataDir returns the directory for persistent data files. Creates the directory if it doesn't exist.

func DataFile

func DataFile(filename string) (string, error)

DataFile returns the path to a data file.

func GetAppName

func GetAppName() string

GetAppName returns the configured application name.

func SetAppName

func SetAppName(name string)

SetAppName sets the application name used for XDG directories. This should be called once at startup from your single source of truth. The name should be lowercase and without spaces (e.g., "ckeletin-go").

func StateDir

func StateDir() (string, error)

StateDir returns the directory for state files (logs, history, etc.). Creates the directory if it doesn't exist.

func StateFile

func StateFile(filename string) (string, error)

StateFile returns the path to a state file.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL