Skip to content

Instantly share code, notes, and snippets.

@miguelmota
Last active April 11, 2022 00:33
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save miguelmota/f30a04a6d64bd52d7ab59ea8d95e54da to your computer and use it in GitHub Desktop.
Save miguelmota/f30a04a6d64bd52d7ab59ea8d95e54da to your computer and use it in GitHub Desktop.
Golang get user home directory path
package main
import (
"fmt"
"os"
"runtime"
)
func userHomeDir() string {
if runtime.GOOS == "windows" {
home := os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH")
if home == "" {
home = os.Getenv("USERPROFILE")
}
return home
} else if runtime.GOOS == "linux" {
home := os.Getenv("XDG_CONFIG_HOME")
if home != "" {
return home
}
}
return os.Getenv("HOME")
}
func main() {
fmt.Println(userHomeDir()) // /Users/mota
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment