Question

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?

Was it helpful?

Solution

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)

OTHER TIPS

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.

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.

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.).

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) {
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top