需要验证和清除用户输入的路径。当用户输入以下命令行时:
app.exe -f "C:\dir with space\"
标志值的最后一个引号已转义,因此它的字符串值为:
C:\dir with space"
对于清理目录/路径的用户输入,你们有什么推荐的干净方法?正则表达式,或者Go是否具有类似于filepath.Clean()的库来处理此问题,但是会删除尾随引号?
编辑:原因记录在这里:https://github.com/golang/go/issues/16131
答案 0 :(得分:1)
例如,
package main
import (
"fmt"
"path/filepath"
"runtime"
"strings"
)
func clean(path string) string {
if runtime.GOOS == "windows" {
path = strings.TrimSuffix(path, `"`)
}
return filepath.Clean(path)
}
func main() {
path := `C:\dir with space"`
fmt.Println(path)
path = clean(path)
fmt.Println(path)
}
输出:
C:\dir with space"
C:\dir with space
答案 1 :(得分:0)
scale