2

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?

Charles
  • 50,943
  • 13
  • 104
  • 142
laurent
  • 88,262
  • 77
  • 290
  • 428
  • 1
    For each of the Windows standard folders, what is the corresponding Linux folder and the corresponding OS-X folder? [Windows Known Folders](http://msdn.microsoft.com/en-us/library/windows/desktop/dd378457.aspx) – peterSO Aug 05 '13 at 03:09
  • I think @peterSO's point is that there may not be a good analogue for each Linux system folder in Windows, or for Windows in Mac, etc. System folders aren't normally considered as variants on the same thing in different systems the way that, say, files are. For example, on Windows and Linux, while the interface for dealing with files may be slightly different, they're basically the same underlying concept with the same basic actions - read and write. System folders, on the other hand, aren't quite that analogous to one another. – joshlf Aug 05 '13 at 03:21
  • I'm aware of the fact that not all special folders are available on all systems. But for instance pretty much any system out there as a "temp" or "home" folder for the current user. Indeed many libraries, such as Qt, provide a way to access these folders in a cross-platform way (http://qt-project.org/doc/qt-5.0/qtcore/qstandardpaths.html). I guess it doesn't yet exist in Go. – laurent Aug 05 '13 at 03:43

5 Answers5

4

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.

Bayu
  • 2,340
  • 1
  • 26
  • 31
  • 1
    I wonder if there is a go module that is similar to https://crates.io/crates/dirs – DrSensor Jun 16 '21 at 02:15
  • 1
    @DrSensor, a good comment. I was looking for such package for golang. A wrapper to a low level directory creation which is cross platform supported. But with no luck. Please let me know if you found one :) – Bayu Jun 17 '21 at 12:28
3

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)
laurent
  • 88,262
  • 77
  • 290
  • 428
2

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.

Community
  • 1
  • 1
Luke
  • 13,678
  • 7
  • 45
  • 79
2

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) {
hedisam
  • 521
  • 6
  • 10
1

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

justinas
  • 6,287
  • 3
  • 26
  • 36