Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Go 尝试检查路径时出错_Go - Fatal编程技术网

Go 尝试检查路径时出错

Go 尝试检查路径时出错,go,Go,我正在尝试在我的golang应用程序中检查windowsdir。 这是我的密码 func createWalletDirectory(path string) (err error) { _, err = os.Stat(path) if os.IsNotExist(err) { return err } path = filepath.FromSlash(path) path = path + string(os.PathSepar

我正在尝试在我的golang应用程序中检查
windows
dir。 这是我的密码

func createWalletDirectory(path string) (err error) {
    _, err = os.Stat(path)

    if os.IsNotExist(err) {
        return err
    }

    path = filepath.FromSlash(path)

    path = path + string(os.PathSeparator) + DirectoryName

    err = os.Mkdir(path, 0666)

    return
}
在函数的第一行,我得到一个错误,如下所示

字符串转义码中的无效字符“i”

示例路径:
C:\Users

注意:我通过POST请求从用户处获得的路径 所以我需要编写一个代码来检查跨平台路径。
如何解决此错误?

在用双引号括起来的Go字符串中,反斜杠启动转义码,例如
\n
\u2318
。要避免这种情况,您有两种选择:

  • 使用双反斜杠(
    \\
    ),例如
    “C:\\Users”
  • 使用倒勾(
    `
    )而不是双引号来定义“原始字符串”,例如
    `C:\Users`

您可以使用
path
包处理URL(“
path/filepath
”用于文件路径),这也有助于平台独立性。因此,您可以执行以下操作来创建路径

givenPath = filepath.Join(DirectoryName, path)
还有另一种方法可以做到这一点

path := strings.Join([]string{DirectoryName, path}, string(os.PathSeparator))