Why escaping exists
Some characters have a special meaning inside a Go string literal. The double quote ends the string, and the backslash starts an escape sequence. To put one of these characters into the text itself, you write a backslash followed by a code. The pair is replaced by the real character when the program runs.
Go reads the literal while compiling, so the escapes affect only what you type in the source code. The string in memory holds the actual characters.
The common escape sequences
| Write | You get |
|---|---|
\" | a double quote |
\\ | one backslash |
\n | a new line |
\t | a tab |
Escape sequences are single characters in the final string: len("\n") is 1.
Quotes inside a string
A double quote inside a double-quoted string would end the literal early, so it is written \". An apostrophe needs no escape, because it has no special meaning in a double-quoted string:
fmt.Println("I'll study \"Coding 101\" on AlgoCademy")
This prints I'll study "Coding 101" on AlgoCademy, with the inner quotes included and the apostrophe unchanged.
New lines in one string
The sequence \n ends the current line. One fmt.Println call can print two lines:
fmt.Println("I will learn this\nNo matter what")
The output has two lines:
I will learn this
No matter what
Println still adds its own line break after the whole text, so the output ends with a new line as usual.
Backslashes
A single backslash starts an escape sequence, so a real backslash has to be doubled. This is common in Windows file paths:
fmt.Println("C:\\Users\\Andy\\notion")
The output is C:\Users\Andy\notion. Go checks the escapes when it compiles. A backslash followed by a character that is not a valid escape, such as the \U in "C:\Users", is a compile error (the compiler reports an invalid hexadecimal escape) rather than a quiet mistake. A path like "C:\new" is worse in a way: \n is valid, so it compiles and puts a line break in the path.
Tabs and other characters
The sequence \t inserts a tab, which is handy for lining up columns of output. You can combine escapes freely in one literal: "name\tscore\n" is a tab between two words and then a line break. Printing with %q shows a string in escaped form, so fmt.Printf("%q\n", "say \"hi\"\n") prints "say \"hi\"\n", which is useful when you cannot tell what characters a string really holds.
Raw strings as an alternative
Go has a second kind of string literal, written between backticks. A raw string does no escape processing, so backslashes mean themselves, and it may span several lines:
fmt.Println(`C:\Users\Andy\notion`)
This prints the same path. Raw strings are convenient for paths and patterns, but a backtick cannot appear inside one. This lesson practices the escaped form, so it is the one the exercise uses.
The lesson's exercise
- Quotes. Print
I'll study "Coding 101" on AlgoCademywithfmt.Println. Escape the two inner double quotes as\"so they do not end the string. The apostrophe stays as it is. - Two lines, one call. Print
I will learn thisandNo matter whaton separate lines using a singlefmt.Println, with\nbetween the texts. - The path. Print
C:\Users\Andy\notionby doubling each backslash, so that none starts an escape sequence.
fmt.Println("I'll study \"Coding 101\" on AlgoCademy")
fmt.Println("I will learn this\nNo matter what")
fmt.Println("C:\\Users\\Andy\\notion")
Common mistakes
- Escaping the apostrophe. In a double-quoted string,
\'is not a valid escape and is rejected by the compiler. - Leaving a quote unescaped. The string ends there and the rest becomes a syntax error.
- Using a single backslash in a path. Depending on the next letter, it fails to compile or silently inserts a special character.
- Typing
/nfor a new line. The escape uses a backslash. A forward slash prints as-is.
Key takeaways
- A backslash starts an escape sequence in a double-quoted Go string.
\"is a quote,\na new line and\\a backslash.- Apostrophes need no escape inside double quotes.
- Backtick strings are raw and ignore escapes.