WEBVTT 0 00:01.010 --> 00:02.360 Welcome back. 1 00:02.420 --> 00:09.220 In this lecture I'm going to show you what happens when you embed a type with methods to another type 2 00:10.910 --> 00:14.290 Here is a little warning before you continue. 3 00:14.560 --> 00:20.890 If you get confused at any moment of this lecture please check out the struct embedding lecture in the 4 00:20.890 --> 00:22.900 structs section. 5 00:22.900 --> 00:27.330 After that everything will be clear. 6 00:27.380 --> 00:29.540 Let's take a look at these types. 7 00:29.870 --> 00:38.300 They all have almost the same set of fields and methods they all use the title and price fields and 8 00:38.300 --> 00:45.870 they all use the print methods only the game and toy types have a discount method. 9 00:45.870 --> 00:49.640 I might want to make a discount for every type of product. 10 00:49.890 --> 00:54.120 So I'm going to add a discount to the book and puzzle types as well. 11 00:54.270 --> 00:57.350 Now all the types almost look alike. 12 00:57.390 --> 01:02.390 The only difference between them is the published field of the book type. 13 01:02.550 --> 01:03.660 But that's okay. 14 01:03.780 --> 01:05.250 It can stay. 15 01:05.250 --> 01:10.370 Let's invent another type with these common fields and methods. 16 01:10.380 --> 01:11.680 Okay great. 17 01:11.700 --> 01:14.980 Every product has a title and price fields. 18 01:15.070 --> 01:18.220 Also a print and discount methods. 19 01:18.230 --> 01:24.470 Let's embed the product type to each product type. 20 01:24.600 --> 01:28.150 Now all the types here embed a product type. 21 01:28.350 --> 01:37.140 So the book puzzle game and toy types can use the fields and methods of the product type as if they're 22 01:37.140 --> 01:40.400 on let's implement this in the coding editor. 23 01:40.410 --> 01:46.230 First, I'm going to open the toy type here. 24 01:46.350 --> 01:52.930 I'm going to copy everything then I'm going to create a new file for the product type here. 25 01:53.150 --> 01:57.720 I'm going to replace the toy word with the product word. 26 01:58.020 --> 02:07.160 Let's also change the receivers to P. Short for a product. When the other types embed this type, 27 02:07.290 --> 02:15.240 all these fields and methods will be available to them so they will be able to use them as if they're 28 02:15.270 --> 02:16.310 on. 29 02:16.410 --> 02:17.430 That's it. 30 02:17.430 --> 02:20.330 Now it's time to embed the product type. 31 02:20.340 --> 02:22.450 Let's start with the book type. 32 02:22.680 --> 02:30.300 The new product type already has a title and price so I'm going to delete them from the book type. 33 02:30.300 --> 02:37.150 Instead, I'm going to embed the product type. The product type has a print method as well. 34 02:37.350 --> 02:39.710 Should I remove it from here? 35 02:39.720 --> 02:47.760 No, I shouldn't because the book type needs to print a publishing date but the product type doesn't 36 02:47.760 --> 02:49.690 know how to print it. 37 02:49.690 --> 02:58.500 Instead, I'm going to call the print method of the product type as you can see you can run the method 38 02:58.590 --> 03:06.510 of an embedded type as well. The product type will print the title and the price of the book. 39 03:06.510 --> 03:09.950 It's because it's been embedded in this book type. 40 03:10.230 --> 03:19.320 So I'm going to remove these fields from here. For now, I am only going to add a tab character here. 41 03:19.590 --> 03:22.890 You will see what it looks like in a minute. 42 03:22.890 --> 03:30.460 One last thing I need to do here the product type uses pointer receivers. 43 03:30.510 --> 03:32.480 It's better to be consistent. 44 03:32.490 --> 03:37.080 So let's change the book type method to pointer receivers as well. 45 03:39.870 --> 03:48.000 Let's continue with the game type. The product type has all the fields and methods of this type. 46 03:48.000 --> 03:50.610 So let's remove all of them. 47 03:50.610 --> 03:53.900 Instead I'm going to embed the product type. 48 03:54.060 --> 03:55.200 That's all. 49 03:55.200 --> 03:56.380 Don't worry though. 50 03:56.400 --> 04:03.630 Thanks to the embedding the game type will have the fields and methods of the product type. 51 04:03.840 --> 04:06.780 Let's do the same thing for the puzzle type. 52 04:07.350 --> 04:11.110 Let's remove all the fields and methods. 53 04:11.190 --> 04:14.340 I'm going to embed the product type instead. 54 04:14.340 --> 04:17.460 Lastly let's go to the toy type. 55 04:17.520 --> 04:20.380 Let's do the same thing again. 56 04:20.430 --> 04:26.460 So I'm going to remove all the fields and methods and embed the product type. 57 04:26.490 --> 04:28.490 Okay we're almost ready. 58 04:29.160 --> 04:34.240 However as you can see there is a lot of errors in the main function. 59 04:34.350 --> 04:42.400 It's because since I embedded the product type I need to change these composite literals first. 60 04:42.480 --> 04:49.680 I'm going to remove all the field names this step wasn't necessary but I'm trying to keep the code 61 04:49.770 --> 04:51.150 concise. 62 04:51.200 --> 04:51.610 OK. 63 04:51.660 --> 04:55.860 Now I'm going to embed the fields using the product type. 64 04:56.460 --> 05:05.150 This second step was necessary because all the types embed the product type there is still an error. 65 05:05.180 --> 05:06.990 Let me tell you why. 66 05:07.100 --> 05:13.750 As you know the print method on the product type uses a pointer receiver. 67 05:14.060 --> 05:19.550 So I need to take the addresses of the book and puzzle values. 68 05:19.550 --> 05:22.820 So let's go to the main function here. 69 05:22.850 --> 05:31.140 I'm going to make the book and puzzle types pointer values like so there is only a single error left. 70 05:31.190 --> 05:38.440 Remember the third book doesn't have a publishing date. Since I'm not using the field names, 71 05:38.510 --> 05:46.790 I need to pass the published field value myself. So, I'm going to pass a nil value here. 72 05:46.790 --> 05:47.340 Nice. 73 05:47.360 --> 05:48.980 Errors are gone. 74 05:49.010 --> 05:50.840 Let's run the program. 75 05:50.840 --> 05:51.370 Good. 76 05:51.380 --> 05:52.290 It works. 77 05:52.400 --> 05:56.780 However, the publishing dates don't look cute. 78 05:56.780 --> 05:58.670 I will fix them afterward. 79 05:58.670 --> 06:00.800 Don't worry about them for now. 80 06:01.340 --> 06:01.960 OK. 81 06:02.090 --> 06:11.760 As an example let's create a toy variable as you can see the toy doesn't have any fields or methods. 82 06:11.850 --> 06:20.940 However, when I list its fields and methods, there happens to be many of them. It's because there are the 83 06:20.940 --> 06:24.660 product types' fields and methods. 84 06:24.660 --> 06:28.890 The toy type has embedded all of them automatically. 85 06:28.890 --> 06:30.060 Let me show it. 86 06:30.390 --> 06:32.360 I'm going to print it like this. 87 06:34.020 --> 06:43.320 As you can see this toy value only contains a product value the toy value doesn't have any fields but 88 06:43.460 --> 06:50.880 a product field. Every field and method comes from the product type. 89 06:50.890 --> 06:53.980 Let's take a look at a book value instead. 90 06:54.250 --> 06:59.890 It has a product field that contains title and price fields. 91 06:59.890 --> 07:08.290 However, the book also has a publishing date field. So, it takes only some part of its fields from the 92 07:08.290 --> 07:12.960 product the book type has a print method. 93 07:13.100 --> 07:18.670 It embeds a product type and the product has a print method as well. 94 07:18.670 --> 07:25.140 When you print a book, Go will run the book type's print method first. 95 07:25.190 --> 07:29.980 So the book type as the parent type takes priority. 96 07:30.020 --> 07:33.790 The book can also run the print method of the product. 97 07:33.890 --> 07:42.770 So the product print can use the title and price fields to print itself. 98 07:42.770 --> 07:46.470 Lastly, let's take a look at the list type. 99 07:46.720 --> 07:52.540 Since I embed the product type in all the product types, I can change 100 07:52.540 --> 08:00.530 this discount method. I mean I don't have to check for the discount method all the product types already 101 08:00.590 --> 08:02.660 have a discount method. 102 08:02.660 --> 08:08.470 So I'm going to call the discount method directly like this but it doesn't work. 103 08:08.680 --> 08:16.250 It's because the list can only work with printer interface values but the printer interface doesn't 104 08:16.250 --> 08:20.300 have a discount method to solve this problem. 105 08:20.300 --> 08:24.770 I'm going to add a discount method to the printer OK. 106 08:24.790 --> 08:29.570 The error is gone but the printer interface not only prints, 107 08:29.620 --> 08:35.950 it also discounts. So it is better to change its name to item. 108 08:35.950 --> 08:41.940 Now the list type can store any type that has a print and a discount method. 109 08:41.980 --> 08:49.180 As I said, all the products have these methods already. So, they satisfy the item interface 110 08:49.180 --> 08:49.420 now. 111 08:52.180 --> 08:57.690 Let's run this program. As you can see it still works. 112 08:57.690 --> 09:01.830 This is one of the benefits of implicit interfaces. 113 09:01.830 --> 09:10.350 The book puzzle game and toy types don't know anything about the item interface. 114 09:10.440 --> 09:13.580 They satisfy it implicitly. 115 09:13.620 --> 09:23.080 That's why changing the interface from printer to item didn't break anything. Congrats! 116 09:23.080 --> 09:25.200 This was the interfaces. 117 09:25.270 --> 09:26.860 See you in the next lecture.