Start learning free

LESSON 55 OF 338 · STRINGS I

Escaping Characters in Go: Quotes, Newlines and Backslashes

Lesson Read the full explanation

Some characters have special meaning inside Go string literals. To include them as regular characters, we escape them with a backslash (\).

For example, a double quote inside a double-quoted string must be written as \":

fmt.Println("I'll study \"Coding 101\" on AlgoCademy")

Common escape sequences include:

  • \" for a double quote inside a string
  • \n for a new line
  • \\ for a literal backslash
fmt.Println("I will learn this\nNo matter what")
fmt.Println("C:\\Users\\Andy\\notion")

Assignment
Follow the Coding Tutorial and practice escaping quotes, newlines, and backslashes.


STEP 1 OF 3
    Ctrl + Enter to check

    GO · STRINGS I

    Escaping Characters in Go, explained

    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

    WriteYou get
    \"a double quote
    \\one backslash
    \na new line
    \ta 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

    1. Quotes. Print I'll study "Coding 101" on AlgoCademy with fmt.Println. Escape the two inner double quotes as \" so they do not end the string. The apostrophe stays as it is.
    2. Two lines, one call. Print I will learn this and No matter what on separate lines using a single fmt.Println, with \n between the texts.
    3. The path. Print C:\Users\Andy\notion by 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 /n for 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, \n a new line and \\ a backslash.
    • Apostrophes need no escape inside double quotes.
    • Backtick strings are raw and ignore escapes.
    Keep going with GoAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free