【发布时间】:2013-07-10 16:34:01
【问题描述】:
我有一个程序接受将在其中创建文件的目标文件夹。我的程序应该能够处理绝对路径和相对路径。我的问题是我不知道如何将~ 扩展到主目录。
我扩展目的地的功能如下所示。如果给定的路径是绝对路径,则它什么也不做,否则它将相对路径与当前工作目录连接。
import "path"
import "os"
// var destination *String is the user input
func expandPath() {
if path.IsAbs(*destination) {
return
}
cwd, err := os.Getwd()
checkError(err)
*destination = path.Join(cwd, *destination)
}
由于path.Join 不会扩展~,因此如果用户将~/Downloads 之类的内容作为目的地传递,则它不起作用。
我应该如何跨平台解决这个问题?
【问题讨论】: