Search code examples
windowsgo

How to get system root directory for Windows in Google Golang?


Under Windows, the system root directory could be like C:// or D:// (when the OS is installed in driver D:/). How to get this folder in Go?


Solution

  • You could use filepath.VolumeName(os.Getenv("SYSTEMROOT")) + "\\" or the shorter os.Getenv("SYSTEMDRIVE") + "\\". The windir environment variable probably shouldn't be used anymore honestly since it's not a system-controlled environment variable.

    Source for the mentioned environment variables