Home > Software design >  why cant I write a windows filepath like this in golang ?
why cant I write a windows filepath like this in golang ?

Time:03-03

I am trying to read a file on windows in golang. The path is C:\Users\lenovo\Downloads\1.jpeg, and I write like this:

filepath := "C:\Users\lenovo\Downloads\1.jpeg"

This declaration and assignment is illegal by itself as it is marked red in vscode. I've been reading about how golang uses filepath package to handle path issues, but it does not cover the situation where \ is the separator.

By the way, if a \ is added after each \ in the statement above, it works.

CodePudding user response:

I don't use vscode but I know \ is an escape character.

And there is no meaning for \U, \l, and \D.

So you should use,

filepath := "C:\\Users\\lenovo\\Downloads\\1.jpeg"

CodePudding user response:

Alternatively use backticks for a raw string where nothing is escaped:

path := `C:\Users\lenovo\Downloads\1.jpeg`
  • Related