Hi! Welcome. You're going to learn about escape sequences.
Let's get started.
Escape sequences are not specific only to Printf.
I'll use Println to show you how they work. Now, I'm going to show you what happens
when you don't use an escape sequence. Let's print a string literal like this.
OK now, let's run it.
As you can see, it just prints: "hihi". Let's say that, I want to print each "hi" here on a separate line.
Now, I'm going add an escape sequence in between these words like this.
As you can see, Go prints them on separate lines.
Right?
This is because Go interprets string values and, when it sees escape sequences, it prints them differently.
Here when it sees "\n", it adds a new line after the first "hi". OK.
Now let's take a look at the parts of this in detail to understand how it works.
Escape sequences carry special meaning as in here. "\n" here is used for printing a new line.
"\n" here is called an escape sequence.
Let's count the characters inside this string literal.
There are six characters, right?
It's because "\n" escape sequence looks like two characters,
but, actually, it has only one character. So this string literal's length is actually 5 not 6.
Go understands whether this is an escape sequence or not by looking at its first character. When a character
starts with a backslash character,
Go thinks that it might be an escape sequence.
For example, if it's a new line escape sequence as in here, then Go will convert "\n" escape sequence
to a new line character. "\s" here is called an escape character.
There are other escape sequences as well.
For example, you learned that "\n" produces a newline character. You learned that when Go sees a
backslash
in a string, it thinks that, it might be the beginning of an escape sequence, and then it starts interpreting it.
Right.
So what if you want to print a backslash only.
You just need to type the backslashes twice. This tells Go that the backslash is not an escape character,
but just a backslash.
So Go will interpret it as a single backslash character instead.
Here is another question: What if you want to actually store a double-quotes character inside a string literal?
How can you do that?
You know, a string literal is wrapped between double-quotes. To do that, you just need to tell Go
that what you really want is a double-quotes character by typing \". So when Go sees this, it
won't think that you're closing your string literal. It will interpret it as a double-quotes character instead.
For example, here, I've used another backslash just before the "\n" character.
So when Go interprets this string, it won't print a new line. Instead, it will print a backslash.
There are two double-quotes inside this string, right? Here,
I want to print the second "hi" in double-quotes, so that I use two more backslashes
right before the double-quotes here.
OK, let's take a look at how Go interprets this string literal.
Let's run it now.
As you can see, it just prints \n instead of a new line. And, it prints double-qoutes without backslashes.
If I've typed the double-quotes without using backslashes, Go would be confused, and there would be a syntax
error.
As an exercise, remove the backslashes inside this string literal, and see what happens.
Congrats! You've learned about the escape sequences.
See you in the next lecture.