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?
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 is not a system-controlled environment variable.
You may use os.Getenv to get the value for "environment" variable windir. An example is below:
package main
import "os"
import "fmt"
func main() {
fmt.Println("system dir: ", os.Getenv("windir"))
}