Go HomeDir reads /etc/passwd, not $HOME

Run into this issue and found a different solution for go1.12+.

home, _ := os.UserHomeDir()
fmt.Println(home)

On Unix, including macOS, it returns the $HOME environment variable. On Windows, it returns %USERPROFILE%. On Plan 9, it returns the $home environment variable.

1 Like