Standard library / os

os.UserHomeDir()

Returns the current user's home directory using platform-specific environment conventions. Not a lookup for another account. Example intended for Linux/AMD64 with Go 1.23.2 in a disposable working directory; other platforms may differ. Expected stdout: '/bytemind/home\n'.

Formal Syntax & Structure

go syntaxgo
func UserHomeDir() (string, error)

Return Value

(string, error)

Returns the current user's home directory using platform-specific environment conventions.

Working Example

Example Snippetgo
package main
import (  "fmt"  "os")
func must(e error) {  if e != nil {    panic(e)  }}func main() {  must(os.Setenv("HOME", "/bytemind/home"))  p, e := os.UserHomeDir()  must(e)  fmt.Println(p)}
Go Reference