How to resolve user environment variables in filepath
在Windows上使用Golang。尝试使用
1 | os.Open("%userprofile%\\\\myfile.txt") |
获取
要获取文件句柄并使程序也更易于移植,请尝试
1 2 | userprofile := os.Getenv("USERPROFILE") f, err := os.Open(path.Join(userprofile,"myfile.txt")) |
代替
只需编写一个程序包即可。欢迎反馈
https://gitlab.com/stu-b-doo/windowspathenv/
1 2 3 | fmt.Println(windowspathenv.Resolve("%USERPROFILE%/Documents")) // C:\\Users\\YourName\\Documents |
1 2 | userprofile := os.Getenv("USERPROFILE") os.Open(userprofile+"\\\\myfile.txt") |
看看http://www.golangprograms.com/how-to-set-get-and-list-environment-variables.html
您可以阅读\\'userprofile \\'环境值并在将路径传递给os.Open
之前构建路径