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 ?
In Go strings enclosed by double quotes, a backslash starts an escape code, e.g. or
\u2318
. To avoid this, you have two options:
\\
), e.g. "C:\\Users"
`
) instead of double quotes to define a "raw string", e.g. `C:\Users`
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))