1

I am trying to make a string variable containing :

"C:\Program Files\Sublime Text 3\sublime_text.exe" C:\Users\User\Desktop\Guess.py

Unfortunately I am not succeeding in doing so. Is there a way to put the text about as is into a variable, double quotes and all?

5
  • You can escape quote characters or use backticks ` for string literals Commented Feb 25, 2018 at 2:31
  • Please can you show me an example since I am new to golang? I understand the backticks. An example for escape quote characters would be appreciated! Commented Feb 25, 2018 at 2:39
  • Escaping quote char not working... play.golang.org/p/b_MwzoR2gR5 Commented Feb 25, 2018 at 2:47
  • Here is the correctly escaped version play.golang.org/p/CMS78oWTyet Commented Feb 25, 2018 at 2:54
  • Thanks Oswin! I really appreciate it! Commented Feb 25, 2018 at 3:12

1 Answer 1

7

In your example string you have characters that need escaping: " and \

fmt.Println("\"C:\\Program Files\\Sublime Text 3\\sublime_text.exe\" C:\\Users\\User\\Desktop\\Guess.py") 

You can also use back quotes to create what is called a raw string which doesn't require escaping those characters.

fmt.Println(`"C:\Program Files\Sublime Text 3\sublime_text.exe" C:\Users\User\Desktop\Guess.py`) 

List of escapes:

\a   U+0007 alert or bell
\b   U+0008 backspace
\f   U+000C form feed
\n   U+000A line feed or newline
\r   U+000D carriage return
\t   U+0009 horizontal tab
\v   U+000b vertical tab
\\   U+005c backslash
\'   U+0027 single quote  (valid escape only within rune literals)
\"   U+0022 double quote  (valid escape only within string literals)

See the official docs.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.