dtu11716 2019-01-28 01:59
浏览 42
已采纳

尝试检查路径时出现错误

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 ?

  • 写回答

2条回答 默认 最新

  • donglin6313 2019-01-28 02:25
    关注

    In Go strings enclosed by double quotes, a backslash starts an escape code, e.g. or \u2318. To avoid this, you have two options:

    • use a double backslash (\\), e.g. "C:\\Users"
    • use backticks (`) instead of double quotes to define a "raw string", e.g. `C:\Users`

    Further reading

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部