Get system folder paths in Go?

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

Get system folder paths in Go?

问题

在Go语言中,可以以跨平台的方式获取系统文件夹的路径吗?例如临时文件夹、"文档"文件夹等。

我找到了ioutil.TempFolder/File,但它们执行的是不同的操作。有什么想法吗?

英文:

Is it possible to get the path of system folders in Go, in a cross-platform way? eg. temp folders, "document" folders, etc.

I found ioutil.TempFolder/File but they do something different. Any idea?

答案1

得分: 4

在2020年,我试图找到类似的东西,但只是用于临时目录跨平台。当我找到这个帖子并阅读了一些答案后,我几乎得出结论,这是不可能的。

但是经过进一步的研究,我发现Go语言已经有了这个功能。就像被接受的答案指出的那样,它位于os包中。根据这份文档:https://golang.org/pkg/os/#TempDir,我们可以通过调用TempDir()函数来获取它。

如果有人试图查看其他操作系统的目录路径,并在这个帖子中遇到困难,我的建议是,请尝试进行进一步的研究。看起来目前Go语言在操作系统目录方面有更完整的功能。

英文:

In the year 2020, I am trying to get similar things, but only for temporary directory cross-platform. When I found this thread and read some answers, I almost make a conclusion that it is not possible.

But after a few further research, I found that go already have it. Just like pointed by the accepted answer, it stands inside os package. Based on this documentation: https://golang.org/pkg/os/#TempDir, we can get it by calling: TempDir() function.

If someone trying to look at another OS system directories path, and stumbled upon in this thread, my suggestion, please just try to have a few further research. Looks like currently go have more complete functions regarding OS system directories.

答案2

得分: 3

目前还没有一种跨平台的方式来访问标准系统文件夹。不过,可以使用user包来访问主目录:

u, _ := user.Current()
fmt.Println(u.HomeDir)
英文:

There's currently no way to access standard system folders in a cross-platform way. The Home directory though can be access using the user package:

u, _ := user.Current()
fmt.Println(u.HomeDir)

答案3

得分: 2

内置选项尚不存在。您最好的选择是提交问题并提出功能请求。

与此同时,您可以通过使用特定于平台的+构建标志来自行添加支持。有几种选择:

  1. 使用os包获取每个系统的信息,可能通过shell进行。
  2. 使用现有的C / C++方法使用cgo。参见此答案,它解释了如何使用C++获取Windows的此信息。

阅读os包的源代码可能也会有所帮助,以了解如何获取特定于平台的信息。这可以帮助您设计一种获取此信息的方法,并可能提交一个补丁以包含在其中。

英文:

A built-in option doesn't exist yet. Your best bet is to open an issue and submit a feature request.

In the meantime you can add support yourself by using platform specific +build flags. With that you have a couple of options:

  1. Use the os package to get the information for each system, possibly through the shell.
  2. Use cgo with existing C / C++ methods. See this answer, which explains how to get this information using C++ for Windows.

It may also be helpful to read the source code of the os package to see how platform-specific information is obtained. This could help you devise a way to get this information, and perhaps submit a patch to be included.

答案4

得分: 2

对于操作系统的临时目录,根据Bayu所说,有一个内置函数os.TempDir() string可以获取操作系统特定的临时目录:

// TempDir返回用于临时文件的默认目录。
//
// 在Unix系统上,如果非空,则返回$TMPDIR,否则返回/tmp。
// 在Windows上,它使用GetTempPath,从%TMP%,%TEMP%,%USERPROFILE%或Windows目录返回第一个非空值。
// 在Plan 9上,它返回/tmp。
//
// 不能保证该目录存在或具有可访问权限。
func TempDir() string {
	return tempDir()
}

实际上,如果您为dir参数提供一个空字符串,ioutil.TempDir(dir, pattern string) (string, error)函数会使用它。请查看第5行和第6行:

// TempDir在目录dir中创建一个新的临时目录。
// 目录名称是通过将pattern应用于末尾生成的随机字符串生成的。如果pattern包含“*”,则随机字符串将替换最后一个“*”。TempDir返回新目录的名称。
// 如果dir是空字符串,则TempDir使用默认目录用于临时文件(参见os.TempDir)。
// 同时调用TempDir的多个程序将不会选择相同的目录。当不再需要时,调用者有责任删除该目录。
func TempDir(dir, pattern string) (name string, err error) {
英文:

For the OS's temp directory, as stated by Bayu, there is a built-in function os.TempDir() string to get the os specific temp directory:

// TempDir returns the default directory to use for temporary files.
//
// On Unix systems, it returns $TMPDIR if non-empty, else /tmp.
// On Windows, it uses GetTempPath, returning the first non-empty
// value from %TMP%, %TEMP%, %USERPROFILE%, or the Windows directory.
// On Plan 9, it returns /tmp.
//
// The directory is neither guaranteed to exist nor have accessible
// permissions.
func TempDir() string {
	return tempDir()
}

which is actually used by the ioutil.TempDir(dir, pattern string) (string, error) function if you provide an empty string for the dir parameter. Check out the 5th and 6th lines:

// TempDir creates a new temporary directory in the directory dir.
// The directory name is generated by taking pattern and applying a
// random string to the end. If pattern includes a "*", the random string
// replaces the last "*". TempDir returns the name of the new directory.
// If dir is the empty string, TempDir uses the
// default directory for temporary files (see os.TempDir).
// Multiple programs calling TempDir simultaneously
// will not choose the same directory. It is the caller's responsibility
// to remove the directory when no longer needed.
func TempDir(dir, pattern string) (name string, err error) {

答案5

得分: 1

除了Luke提到的方法之外,在Windows上,您可以从环境变量中获取一些路径。在某种程度上,Unix也适用(例如$HOME等)。

英文:

Besides the methods Luke mentioned, on Windows you can get some of the paths from environment variables. Same applies, to some extent, to Unix ($HOME, etc.).

huangapple
  • 本文由 发表于 2013年8月5日 10:50:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/18049947.html
匿名

发表评论

匿名网友

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

确定