Skip to content

Instantly share code, notes, and snippets.

@cute-angelia
Forked from miguelmota/userhomedir.go
Created November 4, 2019 13:49
Show Gist options
  • Save cute-angelia/c61f575b4bdf2a68ded6c0af4bb74dd4 to your computer and use it in GitHub Desktop.
Save cute-angelia/c61f575b4bdf2a68ded6c0af4bb74dd4 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