WEBVTT

0
00:00.790 --> 00:04.550
Hi! Welcome. You're going to learn about escape sequences.

1
00:04.730 --> 00:05.760
Let's get started.

2
00:07.310 --> 00:10.720
Escape sequences are not specific only to Printf.

3
00:11.010 --> 00:17.570
I'll use Println to show you how they work.
Now, I'm going to show you what happens

4
00:17.610 --> 00:22.760
when you don't use an escape sequence.
Let's print a string literal like this.

5
00:22.760 --> 00:23.720
OK now, let's run it.

6
00:26.640 --> 00:33.310
As you can see, it just prints: "hihi".
Let's say that, I want to print each "hi" here on a separate line.

7
00:33.310 --> 00:36.960
Now, I'm going add an escape sequence in between these words like this.

8
00:41.260 --> 00:44.070
As you can see, Go prints them on separate lines.

9
00:44.140 --> 00:45.070
Right?

10
00:45.100 --> 00:51.280
This is because Go interprets string values and,
when it sees escape sequences, it prints them differently.

11
00:52.580 --> 00:58.650
Here when it sees "\n", it adds a new line after the first "hi". OK.

12
00:58.650 --> 01:03.420
Now let's take a look at the parts of this in detail to understand how it works.

13
01:04.220 --> 01:12.040
Escape sequences carry special meaning as in here.
"\n" here is used for printing a new line.

14
01:12.050 --> 01:15.170
"\n" here is called an escape sequence.

15
01:15.170 --> 01:18.450
Let's count the characters inside this string literal.

16
01:18.530 --> 01:21.620
There are six characters, right?

17
01:21.680 --> 01:24.660
It's because "\n" escape sequence looks like two characters,

18
01:24.670 --> 01:32.860
but, actually, it has only one character.
So this string literal's length is actually 5 not 6.

19
01:33.650 --> 01:40.200
Go understands whether this is an escape sequence or not by looking at its first character. When a character

20
01:40.190 --> 01:42.040
starts with a backslash character,

21
01:42.130 --> 01:45.380
Go thinks that it might be an escape sequence.

22
01:45.560 --> 01:51.830
For example, if it's a new line escape sequence as in here, then Go will convert "\n" escape sequence

23
01:52.040 --> 01:59.000
to a new line character.
"\s" here is called an escape character.

24
01:59.010 --> 02:01.400
There are other escape sequences as well.

25
02:01.440 --> 02:08.670
For example, you learned that "\n" produces a newline character.
You learned that when Go sees a

26
02:08.730 --> 02:09.420
backslash

27
02:09.460 --> 02:15.860
in a string, it thinks that, it might be the beginning of an escape sequence, and then it starts interpreting it.

28
02:15.870 --> 02:17.380
Right.

29
02:17.460 --> 02:20.910
So what if you want to print a backslash only.

30
02:21.240 --> 02:27.630
You just need to type the backslashes twice.
This tells Go that the backslash is not an escape character,

31
02:27.990 --> 02:29.430
but just a backslash.

32
02:29.430 --> 02:33.550
So Go will interpret it as a single backslash character instead.

33
02:34.640 --> 02:39.890
Here is another question: What if you want to actually store a double-quotes character inside a string literal?

34
02:39.930 --> 02:41.760
How can you do that?

35
02:42.570 --> 02:48.780
You know, a string literal is wrapped between double-quotes.
To do that, you just need to tell Go

36
02:49.050 --> 02:56.980
that what you really want is a double-quotes character by typing \".
So when Go sees this, it

37
02:57.020 --> 03:03.780
won't think that you're closing your string literal.
It will interpret it as a double-quotes character instead.

38
03:04.670 --> 03:09.380
For example, here, I've used another backslash just before the "\n" character.

39
03:09.380 --> 03:16.540
So when Go interprets this string, it won't print a new line. Instead, it will print a backslash.

40
03:16.550 --> 03:20.810
There are two double-quotes inside this string, right?
Here,

41
03:20.830 --> 03:26.240
I want to print the second "hi" in double-quotes, so that I use two more backslashes

42
03:26.270 --> 03:28.740
right before the double-quotes here.

43
03:28.860 --> 03:33.170
OK, let's take a look at how Go interprets this string literal.

44
03:33.380 --> 03:34.090
Let's run it now.

45
03:38.260 --> 03:45.650
As you can see, it just prints \n instead of a new line.
And, it prints double-qoutes without backslashes.

46
03:45.820 --> 03:51.880
If I've typed the double-quotes without using backslashes, Go would be confused, and there would be a syntax

47
03:51.880 --> 03:52.380
error.

48
03:53.580 --> 03:59.450
As an exercise, remove the backslashes inside this string literal, and see what happens.

49
04:01.140 --> 04:04.290
Congrats! You've learned about the escape sequences.

50
04:04.290 --> 04:05.280
See you in the next lecture.