2019-01-28 09:59
浏览 37


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)


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 ?

图片转代码服务由CSDN问答提供 功能建议

我正在尝试检查golang应用程序中的 windows 目录。 这是我的代码

  func createWalletDirectory(路径字符串)(错误错误){
 _,err = os.Stat(path)
如果os.IsNotExist(err){\  n return err 
 path = filepath.FromSlash(path)
 path =路径+字符串(os.PathSeparator)+ DirectoryName 
 err = os.Mkdir(path,0666)



示例路径: C:\ Users \ n

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

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • donglin6313 2019-01-28 10: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

    解决 无用
    打赏 举报
  • douyingyu5573 2019-01-30 07:55

    You can use path package to work with the urls('path/filepath' for the file paths) which also contributes in platform independency. So you can do following to create the path

    givenPath = filepath.Join(DirectoryName, path)

    There is also another way of doing this

    path := strings.Join([]string{DirectoryName, path}, string(os.PathSeparator))
    解决 无用
    打赏 举报

相关推荐 更多相似问题