获取用户的主目录

huangapple go评论86阅读模式
英文:

Obtain user's home directory

问题

以下是获取当前用户的主目录的最佳方法吗?还是有我忽视的特定函数?

os.Getenv("HOME")

如果上述方法是正确的,是否有人知道这种方法在非Linux平台(如Windows)上是否保证可行?

英文:

Is the following the best way of obtaining the running user's home directory? Or is there a specific function that I've ovelooked?

os.Getenv("HOME")

If the above is correct, does anyone happen to know whether this approach is guaranteed to work on non-Linux platforms, e.g. Windows?

答案1

得分: 246

自Go 1.12版本开始,推荐的方式是:

package main
import (
    "os"
    "fmt"
    "log"
)
func main() {
    dirname, err := os.UserHomeDir()
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println(dirname)
}

旧的推荐方式:

在Go 1.0.3版本(可能在更早的版本中也适用)中,以下代码可以工作:

package main
import (
    "os/user"
    "fmt"
    "log"
)
func main() {
    usr, err := user.Current()
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println(usr.HomeDir)
}
英文:

Since go 1.12 the recommended way is:

package main
import (
	"os"
	"fmt"
	"log"
)
func main() {
	dirname, err := os.UserHomeDir()
	if err != nil {
		log.Fatal( err )
	}
	fmt.Println( dirname )
}

Old recommendation:

In go 1.0.3 ( probably earlier, too ) the following works:

package main
import (
	"os/user"
	"fmt"
	"log"
)
func main() {
	usr, err := user.Current()
	if err != nil {
		log.Fatal( err )
	}
	fmt.Println( usr.HomeDir )
}

答案2

得分: 87

os.UserHomeDir()

在go1.12+版本中,你可以使用os.UserHomeDir()

home, err := os.UserHomeDir()

参考链接:https://golang.org/pkg/os/#UserHomeDir

即使没有启用CGO(即FROM scratch),也不需要解析/etc/passwd或其他类似的无用信息,这应该也能正常工作。

英文:

os.UserHomeDir()

In go1.12+ you can use os.UserHomeDir()

home, err := os.UserHomeDir()

See https://golang.org/pkg/os/#UserHomeDir

That should work even without CGO enabled (i.e. FROM scratch) and without having to parse /etc/passwd or other such nonsense.

答案3

得分: 24

例如,

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
	}
	return os.Getenv("HOME")
}

func main() {
	dir := UserHomeDir()
	fmt.Println(dir)
}
英文:

For example,

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
	}
	return os.Getenv("HOME")
}

func main() {
	dir := UserHomeDir()
	fmt.Println(dir)
}

答案4

得分: 4

类似于 @peterSO 的答案,但是对于 Linux,尊重 XDG_CONFIG_HOME 路径。

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())
}
英文:

Similar answer to @peterSO but respects the XDG_CONFIG_HOME path for linux.

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())
}

答案5

得分: 3

这是一种简洁的方法(如果你只在UNIX系统上运行):

import (
  "os"
)

var home string = os.Getenv("HOME")

这只是查询了$HOME环境变量。

--- 编辑 ---

我现在看到上面已经建议了相同的方法。我将保留这个示例作为一个简化的解决方案。

英文:

Here's a nice, concise way to do it (if you're only running on a UNIX based system):

import (
  "os"
)

var home string = os.Getenv("HOME")

That just queries the $HOME environment variable.

--- Edit ---

I now see that this same method was suggested above. I'll leave this example here as a distilled solution.

答案6

得分: 2

你应该在Windows下使用环境变量USERPROFILEHOMEPATH。请参阅Recognized Environment Variables(欢迎提供更合适的文档链接)。

英文:

You should use the environment variable USERPROFILE or HOMEPATH under Windows. See Recognized Environment Variables (a more apropos documentation link would be welcomed).

答案7

得分: 2

go1.8rc2有一个名为go/build/defaultGOPATH的函数,用于获取主目录。
以下代码是从defaultGOPATH函数中提取的。

package main

import (
	"fmt"
	"os"
	"runtime"
)

func UserHomeDir() string {
	env := "HOME"
	if runtime.GOOS == "windows" {
		env = "USERPROFILE"
	} else if runtime.GOOS == "plan9" {
		env = "home"
	}
	return os.Getenv(env)
}

func main() {
	dir := UserHomeDir()
	fmt.Println(dir)
}
英文:

go1.8rc2 has the go/build/defaultGOPATH function which gets the home directory.
https://github.com/golang/go/blob/go1.8rc2/src/go/build/build.go#L260-L277

The following code is extracted from the defaultGOPATH function.

package main

import (
	"fmt"
	"os"
	"runtime"
)

func UserHomeDir() string {
	env := "HOME"
	if runtime.GOOS == "windows" {
		env = "USERPROFILE"
	} else if runtime.GOOS == "plan9" {
		env = "home"
	}
	return os.Getenv(env)
}

func main() {
	dir := UserHomeDir()
	fmt.Println(dir)
}

huangapple
  • 本文由 发表于 2011年10月28日 04:54:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/7922270.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定