WEBVTT 0 00:01.320 --> 00:07.340 Welcome. In this part of the section, I'm going to show you the basics of strings. 1 00:07.350 --> 00:13.440 In the first step, I'm going to talk about the raw string literals. In the second step, you'll learn how to 2 00:13.440 --> 00:20.520 combine strings using the concatenation operator. In the third step, you'll learn about how to get the length 3 00:20.580 --> 00:21.640 of a string. 4 00:22.080 --> 00:27.270 And the last step, I'm going to show you an example program using strings. 5 00:27.270 --> 00:33.090 By the way, in this part of the section, you'll get basic understanding of strings. 6 00:33.090 --> 00:39.070 I'm going to return back to it after the slices section for a more comprehensive explanation. 7 00:39.090 --> 00:40.830 So are you ready? 8 00:40.890 --> 00:42.040 Let's get started. 9 00:43.750 --> 00:44.610 OK. 10 00:44.910 --> 00:47.170 Let's talk about raw string literals first. 11 00:49.910 --> 00:57.890 You already saw the string literal here before. It just encloses the characters within the double quotes. 12 00:58.070 --> 01:02.290 And here's an example of a raw string litera. As you can see here, 13 01:02.350 --> 01:09.920 this time, you need to wrap the characters between back-quotes instead of double-quotes. A string literal cannot 14 01:09.920 --> 01:12.390 contain multiple lines of text. 15 01:12.470 --> 01:18.420 However, unlike a string literal, a raw string literal can contain multiple lines of text. 16 01:20.050 --> 01:25.060 By the way, the first one, a string literal value is being interpreted by Go. 17 01:25.330 --> 01:31.240 For example, if there is a new line escape sequence inside a string literal, Go will convert it to a 18 01:31.240 --> 01:33.370 newline character. 19 01:33.500 --> 01:39.240 However, a raw string literal is an unprocessed string data. 20 01:39.400 --> 01:42.670 So Go doesn't interpret anything in it. 21 01:42.670 --> 01:49.690 It is as it is. For example, if there is a new line escape sequence in it, Go won't see it as an escape 22 01:49.690 --> 01:51.130 sequence. 23 01:51.130 --> 01:54.100 Don't worry though, you're going to see how it works in a minute. 24 01:55.160 --> 01:59.890 The type of a string literal and a raw string literal is the same. 25 02:00.100 --> 02:03.680 They both are strings but they only typed differently. 26 02:04.520 --> 02:08.880 As I said, a string literal is typed between double-quotes. 27 02:08.940 --> 02:12.950 However, a raw string literal is typed between back-quotes. 28 02:17.510 --> 02:20.950 OK now let me show you a few examples in the coding editor. 29 02:25.370 --> 02:29.040 Now, I'm going to declare a new string variable like this. 30 02:29.200 --> 02:32.280 Let's assign it a string literal value like this. 31 02:34.480 --> 02:42.050 Now I'm going to assign it a raw string literal value like this one. As you can see here, I've wrapped it inside 32 02:42.050 --> 02:45.280 back-quotes instead of double-quotes. 33 02:45.500 --> 02:46.380 When I do this, 34 02:46.410 --> 02:52.150 Go thinks that this is a raw string literal. And, I can assign the raw string literal to a string variable. 35 02:52.150 --> 02:54.550 Because a raw string literal's default type is string. 36 02:54.550 --> 02:56.100 Because a raw string literal's default type is string. 37 02:56.530 --> 03:04.160 Exactly the same as the string literal above. 38 03:04.160 --> 03:08.200 Now let's print this variable. 39 03:08.320 --> 03:13.800 Let me show you this in the console. 40 03:13.830 --> 03:17.120 All right looks ok. 41 03:17.320 --> 03:23.180 I told you that a raw string literal can contain multiple lines of text right. 42 03:23.200 --> 03:30.640 Let me show you how useful that can be. First, I'm going to type a small part of HTML code. 43 03:30.640 --> 03:35.620 By the way, web pages are coded with the markup language called HTML. 44 03:36.060 --> 03:45.120 I use \n\t here. It's because I want to print this "body tag" in a new line and I use 45 03:45.160 --> 03:52.420 \t just for the indentation. Then I'm using this \" escape sequence here to print a double-quote. 46 03:52.420 --> 03:56.140 If I used just double-quotes here instead, 47 03:56.140 --> 03:57.910 Go would warn me. 48 03:57.970 --> 03:58.810 Let me show you. 49 04:02.100 --> 04:06.090 As you can see, Go couldn't recognize this as a string literal. 50 04:06.090 --> 04:10.990 Let me put it back. 51 04:11.010 --> 04:11.820 All right. 52 04:11.820 --> 04:12.810 Now I'm going to print it. 53 04:17.330 --> 04:18.860 It's nice and pretty, right? 54 04:20.400 --> 04:24.350 However, this string literal is hard to read and maintain. 55 04:24.360 --> 04:27.230 It's better to use a raw string literal instead. 56 04:27.500 --> 04:31.020 So that I don't need to type escape sequences. 57 04:31.050 --> 04:32.680 Let me show you how to do that. 58 04:33.630 --> 04:36.570 Now I'm going to use a raw string literal. To do that, 59 04:36.570 --> 04:40.720 first, I need to type it inside the back-quotes like this. 60 04:40.800 --> 04:44.780 Then I'm going to type the actual HTML code inside. 61 04:44.970 --> 04:50.030 Here, I am typing it as an HTML code in a nice and pretty formatted way, right? 62 04:51.600 --> 04:56.680 I don't need to worry about escape sequences. As you can see, 63 04:56.710 --> 04:59.080 this string value here contains multiple lines. 64 05:04.230 --> 05:05.090 In the console, 65 05:05.100 --> 05:06.670 it looks like this. 66 05:06.720 --> 05:13.090 Nice and pretty again. And, it's also pretty here, in the code. 67 05:13.090 --> 05:17.340 Now let's compare the above string literal with this raw string literal here. 68 05:18.330 --> 05:23.910 I didn't need to use any escape sequences. It's because Go doesn't process what's inside a raw 69 05:23.910 --> 05:24.440 string literal. 70 05:24.500 --> 05:28.200 Go takes it as it is. As always, 71 05:28.220 --> 05:29.980 there are exceptions. 72 05:29.990 --> 05:32.930 Let's take a look at this example. 73 05:32.940 --> 05:34.520 Here's a file path. 74 05:34.610 --> 05:36.600 This is a Windows file path. 75 05:36.840 --> 05:41.000 In this path, backslashes separate the directories and files. 76 05:41.110 --> 05:48.630 But, in Go, backslash is an escape character. So that's why I need to use this slash slash escape sequences 77 05:48.630 --> 05:49.350 here. 78 05:51.170 --> 05:51.960 Let's take a look at 79 05:51.960 --> 05:58.760 how can I create the same string value. But this time I'm going to use a raw string literal instead. 80 05:59.890 --> 06:02.970 Here, I don't need to escape the backslashes. 81 06:03.010 --> 06:07.010 This is because Go doesn't process what's inside of a raw string literal. 82 06:07.330 --> 06:14.710 So I can use the backslashes without escaping them. I think that these raw string literals look better than a string literal. 83 06:14.710 --> 06:15.970 So I can use the backslashes without escaping them. I think that these raw string literals look better than a string literal. 84 06:28.010 --> 06:29.040 Congrats! 85 06:29.040 --> 06:31.110 That's all for now. See you in the next lecture.