runtime - Runtime Information

Overview

The runtime module provides system information, runtime diagnostics, and path utilities for Go applications.

Functions

CachePanic()

Cache panic and prevent stack overflow.

func CachePanic()

Behavior:

  • Catches panic and prevents stack overflow
  • Writes panic information to stderr
  • Dumps stack trace

CachePanicWithHandle()

Cache panic with custom handler.

func CachePanicWithHandle(handle func(err interface{}))

Parameters:

  • handle - Custom panic handler function

Example:

runtime.CachePanicWithHandle(func(err interface{}) {
    log.Errorf("Panic occurred: %v", err)
    // Custom error handling
})

PrintStack()

Print current stack trace.

func PrintStack()

Example:

func debugFunction() {
    runtime.PrintStack()
}

ExecDir()

Get executable directory.

func ExecDir() string

Returns:

  • Directory containing the executable
  • Empty string if error occurs

Example:

execDir := runtime.ExecDir()
configPath := filepath.Join(execDir, "config.json")

ExecFile()

Get executable file path.

func ExecFile() string

Returns:

  • Full path to the executable
  • Empty string if error occurs

Example:

execFile := runtime.ExecFile()
log.Infof("Running from: %s", execFile)

Pwd()

Get current working directory.

func Pwd() string

Returns:

  • Current working directory
  • Empty string if error occurs

Example:

cwd := runtime.Pwd()
log.Infof("Current directory: %s", cwd)

UserHomeDir()

Get user home directory.

func UserHomeDir() string

Returns:

  • User home directory
  • Empty string if error occurs

Example:

homeDir := runtime.UserHomeDir()
configPath := filepath.Join(homeDir, ".myapp", "config.json")

UserConfigDir()

Get user config directory.

func UserConfigDir() string

Returns:

  • Platform-specific user config directory
  • Empty string if error occurs

Example:

configDir := runtime.UserConfigDir()
appConfigDir := filepath.Join(configDir, "myapp")

UserCacheDir()

Get user cache directory.

func UserCacheDir() string

Returns:

  • Platform-specific user cache directory
  • Empty string if error occurs

Example:

cacheDir := runtime.UserCacheDir()
appCacheDir := filepath.Join(cacheDir, "myapp")

LazyConfigDir()

Get lazygophers config directory.

func LazyConfigDir() string

Returns:

  • User config directory with lazygophers organization

Example:

lazyConfigDir := runtime.LazyConfigDir()
configPath := filepath.Join(lazyConfigDir, "config.json")

LazyCacheDir()

Get lazygophers cache directory.

func LazyCacheDir() string

Returns:

  • User cache directory with lazygophers organization

Example:

lazyCacheDir := runtime.LazyCacheDir()
cachePath := filepath.Join(lazyCacheDir, "cache.db")

Usage Patterns

Application Initialization

func initApp() {
    // Get executable directory
    execDir := runtime.ExecDir()
    
    // Get config path
    configPath := filepath.Join(execDir, "config.json")
    
    // Load configuration
    var cfg Config
    if err := config.LoadConfig(&cfg, configPath); err != nil {
        log.Fatalf("Failed to load config: %v", err)
    }
    
    // Get cache directory
    cacheDir := runtime.LazyCacheDir()
    os.MkdirAll(cacheDir, 0755)
    
    // Initialize application
    app.Init(&cfg, cacheDir)
}

Panic Recovery

func main() {
    defer runtime.CachePanic()
    
    // Application code
    if err := runApplication(); err != nil {
        log.Fatalf("Application error: %v", err)
    }
}

func runApplication() error {
    // Application logic
    return nil
}

Debug Information

func printDebugInfo() {
    log.Infof("Executable: %s", runtime.ExecFile())
    log.Infof("Directory: %s", runtime.ExecDir())
    log.Infof("Working: %s", runtime.Pwd())
    log.Infof("Home: %s", runtime.UserHomeDir())
    log.Infof("Config: %s", runtime.UserConfigDir())
    log.Infof("Cache: %s", runtime.UserCacheDir())
}

Custom Panic Handler

func setupPanicHandler() {
    runtime.CachePanicWithHandle(func(err interface{}) {
        log.Errorf("Panic occurred: %v", err)
        
        // Send alert
        sendAlert(fmt.Sprintf("Panic: %v", err))
        
        // Save stack trace
        saveStackTrace()
        
        // Graceful shutdown
        gracefulShutdown()
    })
}

func sendAlert(message string) {
    // Send alert to monitoring system
}

func saveStackTrace() {
    // Save stack trace to file
    runtime.PrintStack()
}

func gracefulShutdown() {
    // Cleanup resources
    log.Info("Performing graceful shutdown...")
}

Platform-Specific Paths

Linux/Unix

UserHomeDir()    // /home/username
UserConfigDir()  // /home/username/.config
UserCacheDir()   // /home/username/.cache

macOS

UserHomeDir()    // /Users/username
UserConfigDir()  // /Users/username/Library/Application Support
UserCacheDir()   // /Users/username/Library/Caches

Windows

UserHomeDir()    // C:\Users\username
UserConfigDir()  // C:\Users\username\AppData\Roaming
UserCacheDir()   // C:\Users\username\AppData\Local

Best Practices

Panic Handling

// Good: Use defer for panic recovery
func safeFunction() {
    defer runtime.CachePanic()
    
    // Code that might panic
}

// Avoid: Not handling panics
func unsafeFunction() {
    // Code that might panic
}

Path Resolution

// Good: Use runtime functions for cross-platform paths
func getConfigPath() string {
    execDir := runtime.ExecDir()
    return filepath.Join(execDir, "config.json")
}

// Avoid: Hardcoding paths
func getConfigPathBad() string {
    return "/usr/local/myapp/config.json"  // Not cross-platform
}

Debug Information

// Good: Print debug information on startup
func main() {
    printDebugInfo()
    
    if err := runApplication(); err != nil {
        log.Fatalf("Application error: %v", err)
    }
}

func printDebugInfo() {
    log.Infof("Executable: %s", runtime.ExecFile())
    log.Infof("Working: %s", runtime.Pwd())
}