WEBVTT 0 00:03.290 --> 00:04.880 Welcome back. 1 00:05.100 --> 00:09.660 Lastly you have learned how to create your own functions. 2 00:09.660 --> 00:12.840 Now it's time to talk about pointers. 3 00:12.840 --> 00:19.320 At the end of this section you're going to rewrite the log parser project using pointers. 4 00:19.320 --> 00:22.380 You're also going to learn about the pointer semantics. 5 00:22.380 --> 00:30.270 You will understand the implications of passing pointers to struct, map and slice values to functions. 6 00:30.270 --> 00:34.770 So at the end of the section you will understand everything about pointers. 7 00:34.920 --> 00:36.860 All right let's get started. 8 00:37.020 --> 00:43.830 A pointer stores the memory address of a value located in computer memory such as a variable so you 9 00:43.830 --> 00:49.590 can use it to retrieve and update a value through its memory address before going on. 10 00:49.590 --> 00:50.820 Here is a little warning. 11 00:50.820 --> 00:57.150 In this lecture I'm going to use a few variables such as these ones P is short for a pointer variable 12 00:57.420 --> 01:00.030 V is short for a variable. 13 01:00.030 --> 01:04.080 So please don't be confused about why I am using these names. 14 01:04.080 --> 01:07.570 I could have named these variables with any other name. 15 01:07.590 --> 01:10.670 They are not special. 16 01:10.740 --> 01:13.440 Think of this grid as computer memory. 17 01:13.440 --> 01:16.110 You may remember it from the previous sections. 18 01:16.140 --> 01:21.150 Each cell represents a memory location and each one has a memory address. 19 01:21.150 --> 01:23.480 Let's say you want to count the visits. 20 01:23.520 --> 01:27.900 It can be Web site visits or you need to count records in a database. 21 01:27.900 --> 01:29.290 It isn't important now. 22 01:29.490 --> 01:35.490 So we declare and initialize a byte variable like so of course for a counter. 23 01:35.490 --> 01:41.210 It is better to declare it with an int type so that it can represent more values. 24 01:41.220 --> 01:48.390 However for now I use the byte type because each memory cell represents one byte here so it is easier 25 01:48.390 --> 01:50.230 for me to explain the pointers to you. 26 01:50.820 --> 01:53.900 So what happens when you declare a variable. 27 01:54.150 --> 01:55.320 You know the answer. 28 01:55.320 --> 01:57.510 It allocates space on the memory. 29 01:57.510 --> 02:02.070 Like so here the variable's memory location is 6. 30 02:02.160 --> 02:08.030 Or in other words, its memory address is 6 and its value is hundreds. 31 02:08.040 --> 02:12.540 Because I have initialize it with a hundred so far so good. 32 02:12.540 --> 02:14.620 Now let's talk about pointers. 33 02:14.640 --> 02:20.810 I'll explain why you need them later on but for now let's understand the concept of pointers. 34 02:20.920 --> 02:28.080 In Go, you can find the address of a variable like so the ampersand is an address operator you need to 35 02:28.080 --> 02:32.220 type it right before a value to find its memory address. 36 02:32.460 --> 02:36.810 Here I use it to find the memory address of the counter variable. 37 02:37.140 --> 02:39.420 It returns us to memory address 6. 38 02:39.450 --> 02:41.320 So what can you do with it. 39 02:41.370 --> 02:48.810 For example you can store it in a pointer variable like so here I have assigned the memory address of 40 02:48.810 --> 02:55.920 the counter variable to the p variable variable p is a pointer variable because it stores a memory address 41 02:56.220 --> 02:57.050 if you want to. 42 02:57.060 --> 03:02.850 You can assign it a different memory address if you want to, or you can pass it to functions and so on. It as an 43 03:02.940 --> 03:06.330 ordinary variable but it's a pointer variable. 44 03:06.420 --> 03:10.940 The only difference is that it stores a memory address by the way. 45 03:10.980 --> 03:17.730 There is fundamental knowledge that you need to be aware of. In Go, pointers have types too. 46 03:18.030 --> 03:22.520 Actually there is a corresponding pointer type for every type automatically. 47 03:23.040 --> 03:26.430 So what is the type of the variable p. 48 03:26.430 --> 03:31.620 Since it stores the memory address of a byte variable its type looks like this. 49 03:31.620 --> 03:37.980 You can read it like so a pointer to a byte value so you can only store the memory address of a byte 50 03:37.980 --> 03:39.150 value in it. 51 03:39.150 --> 03:46.050 You cannot store the memory address of any other type in it the pointer a variable p knows the address 52 03:46.050 --> 03:47.510 of the counter variable. 53 03:47.610 --> 03:48.170 Right. 54 03:48.180 --> 03:52.900 So this means it can find the value of the counter variable too. 55 03:53.250 --> 03:54.000 To do that. 56 03:54.030 --> 03:59.670 You can use the indirection operator like so like the address operator you need to type it right before 57 03:59.670 --> 04:06.210 a pointer value here variable p is a pointer variable so you need to type the indirection operator right 58 04:06.240 --> 04:07.080 before it. 59 04:07.080 --> 04:15.000 So what happens when you do so? As I've said, it finds the value of the counter variable. It's because the variable 60 04:15.000 --> 04:19.300 p points to the counter variable's address. 61 04:19.480 --> 04:26.740 You can also save the result to a variable like so so what is the type of variable v here. 62 04:27.490 --> 04:35.540 Well it's just another byte variable like the counter variable its type is byte because star P returns 63 04:35.540 --> 04:38.440 you the value inside the counter variable. 64 04:38.440 --> 04:45.410 That's why it is called indirection Operator Please never forget star byte is a pointer type. 65 04:45.470 --> 04:47.760 It says that it's a byte pointer. 66 04:47.990 --> 04:51.440 However star P is the indirection operator. 67 04:51.440 --> 04:53.800 So there is an important difference between them. 68 04:53.840 --> 04:58.220 So when you add a star before a type it becomes a pointer type. 69 04:58.760 --> 05:05.190 However when you type it before a pointer value it returns you the referenced value. 70 05:05.210 --> 05:07.970 This was a simple explanation actually. 71 05:08.060 --> 05:13.140 Now let's take a look at what actually happens behind the scenes first. 72 05:13.220 --> 05:17.810 The address operator finds the address of the counter variable like so. 73 05:17.900 --> 05:23.630 After that, I save the address of the counter variable in a pointer variable. 74 05:23.690 --> 05:31.040 When I do so, Go creates a pointer variable on memory. And it saves the address of the counter variable 75 05:31.130 --> 05:32.380 in the variable p. 76 05:32.510 --> 05:33.750 Like so. 77 05:33.920 --> 05:41.120 So, the P pointer variable stores the memory address of the counter variable. As you can see, there are multiple 78 05:41.120 --> 05:42.270 indirections. 79 05:42.500 --> 05:48.230 The address operator finds the address and the pointer P variable stores the address in the counter 80 05:48.230 --> 05:49.220 variable. 81 05:49.220 --> 05:53.770 Now let's take a look at and understand how the indirection operator works. 82 05:53.960 --> 05:58.260 Let's get the counter variable's value through the P pointer. 83 05:58.340 --> 06:00.770 Okay now let's save it to a variable. 84 06:00.890 --> 06:04.970 When I do so Go creates a new byte variable on memory. 85 06:05.060 --> 06:06.870 For now it is empty. 86 06:06.920 --> 06:09.390 Let's see how Go initializes it. 87 06:09.530 --> 06:12.710 The indirection operator needs to find the pointer value. 88 06:13.460 --> 06:17.240 So it looks for it in the P pointer variable through the pointer. 89 06:17.300 --> 06:22.430 the indirection operator sees that variable "P" points to memory location 6. 90 06:22.580 --> 06:29.000 So, it has got the address. Using the address, the indirection operator goes to the address. When it reaches 91 06:29.000 --> 06:29.460 there. 92 06:29.510 --> 06:33.010 It finds that there is a byte variable in that address. 93 06:33.110 --> 06:37.310 Remember the variable counter is stored at this address. 94 06:37.310 --> 06:44.000 So the indirection operator takes the value inside the variable counter and uses that value to initialize 95 06:44.000 --> 06:51.380 the variable v. In the end, "V" becomes 100. As you can see the indirection operator does a lot of 96 06:51.380 --> 06:56.450 things but it's a very fast operation so do not worry about its performance. 97 06:56.540 --> 06:59.690 Note that V stores a copy of the counter 98 06:59.690 --> 07:05.050 variable's value. So the variable counter and V are different variables. 99 07:05.060 --> 07:09.480 This means that they can change independently. 100 07:09.720 --> 07:17.280 Let me assign a different value to the variable v when I do so it changes the value inside the variable 101 07:17.280 --> 07:17.760 v. 102 07:17.880 --> 07:20.460 Please pay attention to the counter variable. 103 07:20.460 --> 07:26.450 It doesn't change because the counter and V variables are different variables. 104 07:26.520 --> 07:33.330 After I've assigned the V variable's value using the pointer indirection, it doesn't need to use pointer mechanics 105 07:33.390 --> 07:38.340 anymore so the variable v becomes an ordinary byte variable. 106 07:38.520 --> 07:46.310 It gets a byte type because the pointer variable "P" is a byte pointer. Now, let's see what happens if I 107 07:46.310 --> 07:48.590 change the counter like so. 108 07:49.010 --> 07:56.000 So the counter becomes 101. Let's use a pointer indirection to see what the pointer P variable 109 07:56.000 --> 08:02.900 returns. It looks at the memory address that is stored in the variable p. Through that it goes to the 110 08:02.900 --> 08:09.500 memory location 6 in there it finds that the counter variable contains 101. 111 08:09.500 --> 08:12.560 So in the end it returns 101. 112 08:12.560 --> 08:19.560 This means: A pointer variable always points to the latest updated value that it points to. Here, 113 08:19.610 --> 08:26.690 it finds that the counter variable is 101 because 101 is its latest 114 08:26.690 --> 08:27.710 value. 115 08:27.710 --> 08:33.000 Now, let's see, what happens if you change the counter variable through the pointer variable. 116 08:33.140 --> 08:34.900 Let's assign 25. 117 08:35.180 --> 08:39.890 As you can see, you can also use the indirection operator on the left-side of the assignment. 118 08:39.890 --> 08:41.240 Let's see how it works. 119 08:41.270 --> 08:44.950 First it finds the value stored in the pointer. 120 08:44.990 --> 08:48.350 It finds that the pointer variable stores the memory address. 121 08:48.400 --> 08:48.920 6. 122 08:49.310 --> 08:54.440 So it goes to the sixth memory address and finds the counter variable there. 123 08:54.440 --> 08:57.170 Now it's time to do the assignment. 124 08:57.170 --> 09:03.420 When I do so, the value inside the counter variable changes to 25. Let's see what happens 125 09:03.440 --> 09:05.920 when you print the counter variable now. 126 09:06.230 --> 09:11.120 Of course it prints 25 because the counter variable contains 25. 127 09:11.990 --> 09:18.470 So in summary by using the indirection operator you can retrieve an updated value indirectly through 128 09:18.590 --> 09:19.290 a pointer. 129 09:19.340 --> 09:24.280 Here it indirectly updates the value of the counter variable. 130 09:24.330 --> 09:27.980 All right let me summarize what you have learned so far. 131 09:28.050 --> 09:30.030 Here is a byte pointer variable. 132 09:30.030 --> 09:35.300 Its type is byte pointer because there is a star before the byte type. 133 09:35.300 --> 09:37.940 Here is a byte variable. Obviously, 134 09:37.950 --> 09:39.570 its type is byte. 135 09:39.630 --> 09:43.220 This is how you can find the memory address of the counter variable. 136 09:43.380 --> 09:46.400 The Ampersand here is called the address operator. 137 09:46.440 --> 09:50.710 It returns a byte pointer value. So it returns a byte pointer type. 138 09:51.120 --> 09:54.440 It's because the counter's type is byte. 139 09:54.640 --> 10:01.240 You can assign the counter's address to the P pointer variable like so. You can do so because the type 140 10:01.240 --> 10:05.840 of P variable and address of counter is a byte pointer. 141 10:05.890 --> 10:10.240 This is how you can find the value of counter through the P pointer variable. 142 10:10.240 --> 10:12.330 It is called the indirection operator. 143 10:12.370 --> 10:17.860 It returns a copy of the counter's value. So it returns a byte value. 144 10:18.040 --> 10:20.960 It's because the counter's type is byte. 145 10:21.190 --> 10:28.880 Please pause at this moment at let's take a look at this diagram, and think about it. All right. 146 10:28.900 --> 10:31.390 I have only talked about the byte pointers. 147 10:31.390 --> 10:37.650 However it's not the whole picture because there is a corresponding pointer type for every other type. 148 10:37.660 --> 10:43.450 Or, you can declare a struct type. When you do so 149 10:43.510 --> 10:49.660 There happens a corresponding struct pointer type like so or there are pointer types for errors as well 150 10:49.690 --> 10:50.310 and so on. 151 10:50.640 --> 10:51.120 All right. 152 10:51.370 --> 10:52.550 That's all for now. 153 10:52.590 --> 10:53.490 See you in the next lecture.