WEBVTT 00:00.420 --> 00:00.920 Hi. 00:01.050 --> 00:07.730 This is the sixth video of this section pointers structures and interfaces in the previous video. 00:07.770 --> 00:12.190 We looked at arrays slices and maps in this video. 00:12.210 --> 00:17.190 We will learn to declare pointers use and print the values from a pointer. 00:17.250 --> 00:24.330 Then we will see how to declare and use structures and at the end we will explore about interfaces pointers 00:24.540 --> 00:30.510 are the number one source of the headache of every C or C++ programmer but they are one of the main 00:30.510 --> 00:35.580 tools to achieve high performance codes in non garbage collected languages. 00:35.580 --> 00:41.730 Fortunately for us goes pointers have achieved the best of both worlds by providing high performance 00:41.730 --> 00:46.150 pointers with garbage collector capabilities and easiness. 00:46.260 --> 00:54.600 On the other side for its detractors go lax inheritance in favor of composition pointers are hated loved 00:54.840 --> 01:00.040 and very useful at the same time to understand what a pointer is can be difficult. 01:00.060 --> 01:03.110 So let's try with a real world explanation. 01:03.360 --> 01:07.910 As we mentioned earlier in this section a pointer is like a mailbox. 01:07.920 --> 01:10.340 Imagine a bunch of mailboxes in a building. 01:10.410 --> 01:15.870 All of them have the same size and shape but each refers to a different house within the building. 01:16.080 --> 01:22.210 Just because all the mailboxes are the same size does not mean that each house will have the same size. 01:22.260 --> 01:28.380 So the pointers are the mailboxes all of them of the same size and that refer to a house. 01:28.380 --> 01:35.790 The building is our memory and the houses are the types our pointers refer to and the memory they allocate. 01:35.880 --> 01:40.860 If you want to receive something in your house it it's far easier to simply send the address of your 01:40.860 --> 01:47.670 house instead of sending the entire house so that your package is deposited inside but they have some 01:47.670 --> 01:54.480 drawbacks as if you send your address and your house disappears after sending or its type owner change. 01:54.480 --> 01:56.250 You'll be in trouble. 01:56.280 --> 01:58.020 How is this useful. 01:58.020 --> 02:04.920 Imagine that somehow you have a four gigabytes of data in a variable and you need to pass it to a different 02:04.920 --> 02:10.470 function without a pointer the entire variable is cloned to the scope of the function that is going 02:10.470 --> 02:11.370 to use it. 02:11.430 --> 02:18.360 So you'll have eight gigabytes of memory occupied by using this variable twice that hopefully the second 02:18.360 --> 02:23.960 function isn't going to use in a different function again to raise this number even more. 02:24.090 --> 02:30.150 You could use a pointer to pass a very small reference to this chunk to the first function so that just 02:30.150 --> 02:34.780 the small reference is cloned and you can keep your memory usage low. 02:34.860 --> 02:41.130 While this isn't the most academic nor exact explanation it gives a good view of what a pointer is without 02:41.130 --> 02:47.030 explaining what a stack or a heap is or how they work in X 86 architectures. 02:47.280 --> 02:52.740 Pointers and go are very limited compared to C or C++ pointers. 02:52.740 --> 02:58.200 You can't use pointer arithmetic nor can you create a pointer to reference an exact position in the 02:58.200 --> 03:03.610 stack pointers and go can be declared like this here. 03:03.660 --> 03:11.820 No Cole on equal to 5 code represents our 4 gigabyte variable and pointer to number contains the reference 03:12.270 --> 03:15.420 represented by an ampersand to this variable. 03:15.420 --> 03:22.050 It's the direction to the variable let's print the variable point to number which is a simple variable. 03:22.500 --> 03:24.330 So let's run this. 03:24.390 --> 03:25.680 What's that number. 03:25.680 --> 03:31.320 Well the direction to our variable memory and how can I print the actual value of the house. 03:31.320 --> 03:36.660 Well with an asterisk we tell the compiler to take the value that the pointer is referencing which is 03:36.660 --> 03:42.630 our 4 gigabyte variable as you can see we got the output as 5. 03:42.630 --> 03:50.730 Now let's move on to struct a struct is an object and go it has some similarities with classes in P 03:50.910 --> 03:55.750 as they have fields struct can implement interfaces and declare methods. 03:55.830 --> 04:02.700 But for example in go there is not inheritance lack of inheritance looks limiting but in fact composition 04:02.700 --> 04:07.500 over inheritance was a requirement of the language to declare a structure. 04:07.560 --> 04:13.740 You have to prefix its name with the keyword type and suffix with the keyword struct and then you declare 04:13.770 --> 04:16.410 any field or method between brackets. 04:16.410 --> 04:21.210 For example let me show you a piece of code in this piece of code. 04:21.300 --> 04:28.380 We have declared a person structure with three public fields that is name surname and Hobbes and one 04:28.380 --> 04:30.730 private field I.D.. 04:30.960 --> 04:38.460 If you recall the visibility part in this section lowercase fields and go refers to private fields that 04:38.460 --> 04:41.070 are just visible within the same package. 04:41.280 --> 04:46.520 With this struct we can now create as many instances of person as we want. 04:46.620 --> 04:52.740 Now will write a function called get full name that will give the composition of the name and the surname 04:52.770 --> 04:54.680 of the struct it belongs to. 04:54.690 --> 04:56.470 Let's add this function. 04:56.880 --> 05:01.510 Methods are defined similarly to functions in a slightly different way. 05:01.520 --> 05:07.910 There is a person asterisk person that refers to a pointer to the created instance of the struct. 05:07.910 --> 05:09.560 It's like using the keyword. 05:09.560 --> 05:15.480 This in Java or self in Python when referring to the pointing object. 05:15.680 --> 05:22.310 Maybe you are thinking why does person asterisk person have the pointer operator to reflect that P is 05:22.310 --> 05:24.760 actually a pointer and not a value. 05:24.770 --> 05:30.000 This is because you can also pass person by value by removing the pointer signature. 05:30.080 --> 05:35.180 In which case a copy of the value of person is passed to the function. 05:35.180 --> 05:41.480 This has some implications for example any change that you make in P if you pass it by value won't be 05:41.480 --> 05:46.820 reflected in source P but what about our get full name method. 05:46.820 --> 05:53.400 It's console output has no effect in appearance but a full copy was passed before evaluating the function. 05:53.540 --> 06:00.320 But if we modify person here the source person won't be affected and the new person value will be available 06:00.410 --> 06:03.000 only on the scope of this function. 06:03.380 --> 06:08.020 On the main function we create an instance of our structure called P. 06:08.150 --> 06:14.150 As you can see we have used implicit notation to create the variable that is the colon equal to symbol 06:14.660 --> 06:15.680 to set the fields. 06:15.680 --> 06:20.000 We have to refer to the name of the field colon the value and the comma. 06:20.000 --> 06:24.990 Don't forget the comma at the end to access the fields of the instantiated structure. 06:25.040 --> 06:30.010 We just refer to them by their name like P. dots name or P. dot Hobbes. 06:30.040 --> 06:36.590 You use the same syntax to access the methods of the structure like p dot get full name. 06:36.650 --> 06:39.050 Here's the output of this program. 06:39.050 --> 06:45.140 Structures can also contain another structure or composition and implement interface methods apart from 06:45.140 --> 06:45.880 their own. 06:45.950 --> 06:48.770 But what's an interface method. 06:48.770 --> 06:51.620 That's what we'll be discussing next. 06:51.620 --> 06:57.470 Interfaces are essential in object oriented programming in functional programming and especially in 06:57.470 --> 07:04.100 design patterns go source code is full of interfaces everywhere because they provide the abstraction 07:04.100 --> 07:09.350 needed to deliver uncoupled code with the help of functions as a programmer. 07:09.350 --> 07:14.390 You also need this type of abstraction when you write libraries but also when you write code that is 07:14.390 --> 07:18.440 going to be maintained in the future with new functionality. 07:18.440 --> 07:22.490 Interfaces are something difficult to grasp at the beginning but very easy. 07:22.490 --> 07:28.220 Once you have understood their behavior and provide very elegant solutions for common problems we will 07:28.220 --> 07:30.340 use them extensively during this course. 07:30.350 --> 07:33.620 So put special focus on this part of this video. 07:34.610 --> 07:38.350 An interface is something really simple but powerful. 07:38.490 --> 07:42.900 It's usually defined as a contract between the objects that implement it. 07:42.980 --> 07:48.770 But this explanation isn't clear enough in my honest opinion for newcomers to the interface world. 07:48.800 --> 07:53.870 A water pipe is a contract to whatever you pass through it must be a liquid. 07:53.870 --> 07:58.970 Anyone can use the pipe and the pipe will transport whatever liquid you put in it without knowing the 07:58.970 --> 07:59.910 content. 08:00.020 --> 08:06.940 The water pipe is the interface that enforces that the users must pass liquids and not something else. 08:06.960 --> 08:08.830 Let's think about another example. 08:08.990 --> 08:09.980 A train. 08:10.190 --> 08:12.990 The railroads of a train are like an interface. 08:13.010 --> 08:20.060 A train must construct that is implement its width with a specified value so that it can enter the railroad. 08:20.120 --> 08:25.400 But the railroad never knows exactly what it's carrying passengers or cargo. 08:25.400 --> 08:30.020 So for example let's see the aspect of an interface of the railroad. 08:30.020 --> 08:35.710 Let me open the main dot go file and here's the railroad white checker interface. 08:35.900 --> 08:41.840 Now the railroad wide checker is the type our trains must implement to provide information about their 08:41.840 --> 08:48.330 with the trains will verify that the train isn't too wide or too narrow to use its railroads. 08:48.470 --> 08:55.520 And here's the code for this logic the railroad is implemented by an imaginary station object that contains 08:55.520 --> 09:01.400 the information about the width of the railroads in this station and that has a method to check whether 09:01.400 --> 09:07.060 a train fits the needs of the railroad with the is correct sized train method. 09:07.120 --> 09:13.670 The is correct sized train method receives an interface object which is a pointer to a train that implements 09:13.700 --> 09:20.360 this interface and returns a validation between the width of the train and the railroad now we have 09:20.360 --> 09:22.600 created a passenger's train. 09:22.640 --> 09:28.190 It has a field to contain its with and implements our check rails with interface method. 09:28.220 --> 09:33.530 The structure is considered to fulfill the needs of the railroad wide checker interface because it has 09:33.530 --> 09:37.910 an implementation of the methods that the interfaces ask for. 09:37.910 --> 09:45.410 So now we create a railroad of 10 units wide and two trains one of 10 units wide that fit the railroad 09:45.410 --> 09:50.120 size and another of 15 units that cannot use the railroad. 09:50.150 --> 09:52.490 Let's dissect this main function. 09:52.580 --> 09:57.900 First we created a railroad object of 10 units called railroad. 09:58.100 --> 10:04.250 Then two trains of 10 and 15 units with four passengers and cargo respectively. 10:04.250 --> 10:11.090 Then we pass both objects to the railroad method that accepts interfaces of the railroad wide checker 10:11.090 --> 10:12.320 interface. 10:12.320 --> 10:18.260 The railroad itself does not know the width of each train separately but it has an interface that trains 10:18.260 --> 10:24.980 must implement so that it can ask for each with and return a value telling you if the train can or cannot 10:24.980 --> 10:26.810 use the railroads. 10:26.810 --> 10:34.190 Finally let's check the output of the call to print a function so move back to the terminal and run 10:34.190 --> 10:39.850 the command go run main dot go as I mentioned earlier. 10:39.920 --> 10:44.870 Interfaces are so widely used during this book that it doesn't matter if it still looks confusing for 10:44.870 --> 10:49.470 the reader as there will be plenty of examples during this course. 10:49.730 --> 10:54.800 In this video we saw how to use pointers structures and interfaces. 10:54.800 --> 10:58.820 In the next video we will learn about testing and TDD.