首页 > 解决方案 > Getting an error when trying to check path

问题描述

I am trying to check windows dir in my golang app. Here is my code

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
}

So on the first line of the function I am getting an error look like this

invalid character 'i' in string escape code

Example path : C:\Users

Note: The path I am getting from users via POST request So I need to make a code which will check crossplatform paths. How can I solve this error ?

标签: go

解决方案


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

givenPath = filepath.Join(DirectoryName, path)

还有另一种方法可以做到这一点

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

推荐阅读