我有一个程序接受将创建文件的目标文件夹.我的程序应该能够处理绝对路径以及相对路径.我的问题是我不知道如何扩展~到主目录.
我扩展目的地的功能看起来像这样.如果给定的路径是绝对路径,则它什么也不做,否则它将与当前工作目录的相对路径连接起来.
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)
}
Run Code Online (Sandbox Code Playgroud)
由于path.Join不扩展~,如果用户传递类似~/Downloads目的地的东西,它就不起作用.
我该如何以跨平台的方式解决这个问题?