github.com/toplink-cn/moby@v0.0.0-20240305205811-460b4aebdf81/pkg/homedir/homedir.go (about) 1 package homedir 2 3 import ( 4 "os" 5 "os/user" 6 "runtime" 7 ) 8 9 // Get returns the home directory of the current user with the help of 10 // environment variables depending on the target operating system. 11 // Returned path should be used with "path/filepath" to form new paths. 12 // 13 // On non-Windows platforms, it falls back to nss lookups, if the home 14 // directory cannot be obtained from environment-variables. 15 // 16 // If linking statically with cgo enabled against glibc, ensure the 17 // osusergo build tag is used. 18 // 19 // If needing to do nss lookups, do not disable cgo or set osusergo. 20 func Get() string { 21 home, _ := os.UserHomeDir() 22 if home == "" && runtime.GOOS != "windows" { 23 if u, err := user.Current(); err == nil { 24 return u.HomeDir 25 } 26 } 27 return home 28 }