Skip to main content
go-homedir

go-homedir

Cross-platform home directory detection for Go without cgo

Why go-homedir?

The built-in os/user package requires cgo on Darwin systems, which prevents cross-compilation. For the 99% of use cases where you just need the home directory, go-homedir provides a pure Go solution that works everywhere.

Cross-Platform

Works seamlessly on Windows, macOS, Linux, and Plan 9

No cgo Required

Pure Go implementation enables cross-compilation

Built-in Caching

Optimized performance with automatic result caching

Simple API

Just two main functions: Dir() and Expand()

Quick Example

package main

import (
    "fmt"
    "github.com/mitchellh/go-homedir"
)

func main() {
    // Get the home directory
    home, err := homedir.Dir()
    if err != nil {
        panic(err)
    }
    fmt.Println("Home directory:", home)

    // Expand ~ in paths
    path, err := homedir.Expand("~/.config/myapp")
    if err != nil {
        panic(err)
    }
    fmt.Println("Expanded path:", path)
}

Key Features

Automatically detects the home directory on Windows (using USERPROFILE, HOMEDRIVE/HOMEPATH), Unix-like systems (using HOME environment variable and getent), macOS (using dscl), and Plan 9.
Unlike os/user, go-homedir doesn’t require cgo, making it perfect for cross-compilation scenarios where cgo would be problematic.
Results are cached automatically to avoid repeated system calls. You can disable caching globally with the DisableCache variable or clear the cache with Reset().
The Expand() function makes it easy to convert paths like ~/Documents to absolute paths by replacing the tilde with the actual home directory.

Next Steps

Installation

Add go-homedir to your Go project

Quickstart

Get up and running in minutes

API Reference

Explore the complete API documentation

Examples

See real-world usage examples

Build docs developers (and LLMs) love