在Go中获取系统文件夹路径?

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?

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)

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.

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