-
-
Save synsa/f9472fb41fa26e940cf8fe6d921abcce to your computer and use it in GitHub Desktop.
Golang get user home directory path
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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