WEBVTT 0 00:01.140 --> 00:03.130 Welcome to Part Six. 1 00:03.150 --> 00:06.850 You will learn about methods and interfaces. 2 00:06.930 --> 00:09.930 First you will learn the methods. 3 00:09.930 --> 00:13.230 After that you will learn the interfaces. 4 00:13.230 --> 00:23.120 Lastly you will learn the well known interfaces let's start with the methods methods enhance types 5 00:23.140 --> 00:30.390 with additional behavior I will create a couple of types and a few functions. 6 00:30.400 --> 00:37.960 After that I will show you how to convert the code to an object oriented style using methods. 7 00:37.960 --> 00:43.050 Suppose you want to sell books to keep everything organized. 8 00:43.060 --> 00:45.080 I'm going to create a new file. 9 00:45.250 --> 00:51.030 First I'm going to declare a new book type OK. 10 00:51.190 --> 00:54.910 Now I can create a function that prints a book. 11 00:54.910 --> 01:04.380 Lastly I'm going to print the title and price of a given book value it's ready let's use it from the 12 01:04.380 --> 01:05.730 main function. 13 01:06.060 --> 01:14.000 Let's create a book I'm gonna say Moby Dick and price ten dollars. 14 01:14.750 --> 01:20.000 OK let's printed OK let's run the program. 15 01:20.200 --> 01:21.040 Nice. 16 01:21.040 --> 01:22.120 It works. 17 01:22.120 --> 01:27.030 Let's say you decided to sell computer games as well to do that. 18 01:27.070 --> 01:33.350 I will create a game type instead of typing the same code over and over again. 19 01:33.450 --> 01:41.760 Let's duplicate the book type instead and I will change the book type to the game as you can see. 20 01:41.910 --> 01:47.840 You cannot create a function with the same name in the same package. 21 01:47.880 --> 01:55.610 I have declared the print book function before. It belongs to the namespace of package main. 22 01:55.680 --> 01:59.430 So I need to change the name of the function. 23 01:59.430 --> 02:04.560 I also need to change the input parameter to the game type. 24 02:04.560 --> 02:10.450 Lastly I'm going to change the variables B to G okay. 25 02:10.660 --> 02:14.870 As you can see it's very similar to the book type here. 26 02:14.950 --> 02:22.810 I created a game type and a function for printing the game type is ready let's use it from the main 27 02:22.810 --> 02:23.860 function. 28 02:24.100 --> 02:26.080 I'm going to create a new game 29 02:28.960 --> 02:33.650 Let's print it Okay let's run the code. 30 02:34.660 --> 02:35.470 It works. 31 02:35.620 --> 02:36.790 So far so good. 32 02:37.180 --> 02:41.680 As you can see there is a printing function for each type here. 33 02:41.710 --> 02:44.650 This is a functional style of programming. 34 02:44.650 --> 02:51.040 Now we can talk about the problems of this style of programming and reasons why you might want to use 35 02:51.040 --> 02:53.310 methods instead of functions. 36 02:53.530 --> 02:58.860 The book type has only two fields title and price. 37 02:59.080 --> 03:07.480 You can create a book value like this as you can see the Moby Dick variable contains the data but has 38 03:07.480 --> 03:08.940 no behavior. 39 03:09.040 --> 03:16.840 I have written only one function for the book type but assume that there will be two functions. 40 03:16.840 --> 03:23.260 The first one prints a book the last one discounts the price of a book. 41 03:23.260 --> 03:28.900 Both take book values and their primary purpose is to work with books. 42 03:29.110 --> 03:38.810 So why don't we combine the data and behavior together I added the functions to the book type the print 43 03:38.840 --> 03:44.540 book and discount functions have become the methods of the book type. 44 03:44.780 --> 03:52.280 As you can see I no longer need to pass a book value to these methods because Go will automatically 45 03:52.280 --> 03:54.930 pass the value of a book to them. 46 03:55.010 --> 03:56.840 You will see an example shortly. 47 03:56.840 --> 04:01.880 No worries. Now the book type has both data and behavior. 48 04:01.880 --> 04:09.860 The book with its data and behavior better represents a real world book product by the way the methods 49 04:09.890 --> 04:13.690 of a type are called the method set of that type. 50 04:13.730 --> 04:20.950 Here the method set of the book type consists of printing and discounting methods. 51 04:20.960 --> 04:25.040 Let me explain how you can attach methods to a type. 52 04:25.040 --> 04:27.260 Let's take a look at the print book function. 53 04:27.260 --> 04:34.430 We wrote earlier it takes a book value as an argument but the print book function is separate from the 54 04:34.430 --> 04:36.860 book to its composite the separate. 55 04:36.860 --> 04:40.740 You can run it like this. When you run it, 56 04:40.740 --> 04:46.220 the variable Moby Dick is being sent to the printBook function as a copy. 57 04:46.230 --> 04:50.170 Now let's attach the function to the book type. 58 04:50.290 --> 04:54.640 Now the print book function has become a method. 59 04:54.640 --> 04:58.510 Let's talk a little bit about the method declarations syntax. 60 04:58.600 --> 05:03.560 The book argument has become something called a receiver. 61 05:03.610 --> 05:07.170 It's just a fancy name for an input parameter. 62 05:07.240 --> 05:09.510 It's just syntactic sugar. 63 05:09.670 --> 05:15.550 If you are coming from other object oriented languages this term may be a familiar to you. 64 05:15.580 --> 05:22.810 Here we say receiver because a method automatically receives the value of a type. 65 05:22.900 --> 05:32.530 As you know input parameters are variables A receiver is also an input parameter so a receiver is also 66 05:32.650 --> 05:33.660 a variable. 67 05:33.670 --> 05:36.220 Let's take a look at its parts. 68 05:36.220 --> 05:39.930 This is the type of the receiver variable. 69 05:39.990 --> 05:42.660 This is the name of the receiver variable. 70 05:42.660 --> 05:48.280 Now the book type has a print book method since the Moby Dick is also a book type. 71 05:48.330 --> 05:51.350 You can call the print book method on it. 72 05:51.380 --> 05:53.090 When you do so, Go 73 05:53.100 --> 06:02.030 passes the Moby Dick variable to the print method as a hidden argument before the method executes Go 74 06:02.130 --> 06:09.380 copies the value of Moby Dick to the receiver variable B so you can use the variable B to print the 75 06:09.380 --> 06:10.050 book. 76 06:10.070 --> 06:14.190 Let me give you one more tip about the receiver names. 77 06:14.300 --> 06:19.990 We frequently use receiver names so we name them using abbreviations. 78 06:20.540 --> 06:26.660 But you can name them however you want here the printBook method belongs to the book type. 79 06:26.690 --> 06:30.970 Therefore I used only the first letter of the book type. 80 06:31.190 --> 06:35.970 Let me show you how you can attach a method to the book type in the coding editor. 81 06:35.990 --> 06:41.290 Let's start by cutting the first parameter from here. 82 06:42.060 --> 06:45.390 And paste it to the left in parentheses. 83 06:45.600 --> 06:46.450 That's it. 84 06:46.470 --> 06:54.790 Ready as you can see the receiver is actually an input parameter written just before the function name. 85 06:54.890 --> 07:03.940 We use the method syntax to make Go understand that the printBook function is a method of the book type. 86 07:03.950 --> 07:11.670 Now we can use it in the main function but now Go is looking for the print book function in the main 87 07:11.670 --> 07:19.280 package but it cannot find it because the print book function now belongs to the book type. 88 07:19.380 --> 07:27.580 So instead I'm going to use a value of the book type Moby Dick and I will type a dot. 89 07:27.600 --> 07:33.210 Remember the dot operator selects a name from a namespace. 90 07:33.420 --> 07:42.290 Here it selects from the namespace of the book type when I pressed the control and space keys together 91 07:42.620 --> 07:47.660 my editor lists only the fields and methods of the book type. 92 07:47.840 --> 07:50.000 The print book Method is here. 93 07:50.000 --> 07:52.390 I can call it like a function. 94 07:52.430 --> 07:57.410 The only difference is that I need to run it through a value here. 95 07:57.440 --> 08:01.090 I execute it through the Moby Dick variable behind the scenes. 96 08:01.100 --> 08:05.900 Go sends the Moby Dick to the print book method as a receiver. 97 08:05.960 --> 08:10.420 Let's try it as you can see it works as before. 98 08:10.490 --> 08:13.700 Let's create a method for the game type as well. 99 08:13.700 --> 08:21.110 Let's start by cutting the first parameter and paste it within parentheses to the left. 100 08:21.290 --> 08:22.240 That's it. 101 08:22.250 --> 08:25.890 Let's run the methods from the main function. 102 08:25.990 --> 08:29.350 Now the print game method belongs to the game type. 103 08:29.350 --> 08:31.950 That's why you can see it here. 104 08:31.960 --> 08:34.060 Let's try it. 105 08:34.180 --> 08:36.080 It also works as before. 106 08:36.190 --> 08:36.820 Nice. 107 08:36.820 --> 08:40.230 Now let's try it with another game value. 108 08:40.270 --> 08:43.840 I'm going to add a new game value like this. 109 08:44.680 --> 08:46.440 And I'm going to print it as well. 110 08:47.400 --> 08:55.570 Let's try it as you can see when I call the print game method with the Minecraft variable. 111 08:55.730 --> 09:04.040 It uses the Minecraft variable but when I run the same method with the Tetris variable this time it 112 09:04.040 --> 09:06.910 uses the Tetris variable instead. 113 09:07.160 --> 09:14.470 It's because the print game method receives a different game value depending on who is calling it. 114 09:14.480 --> 09:18.870 It's like passing the game variable to the print game function. 115 09:18.890 --> 09:21.650 The only difference is that gold passes. 116 09:21.830 --> 09:24.590 The game value as the receiver. 117 09:24.680 --> 09:28.610 Don't forget the receiver is a variable. 118 09:28.610 --> 09:32.600 I will show you the first benefit of using methods. 119 09:32.810 --> 09:40.550 As I said types have their own namespaces for example since the print book method belongs to the book 120 09:40.550 --> 09:48.620 type I can simply say print I mean I don't need to say print book anymore it's because there can be 121 09:48.620 --> 09:52.490 methods with the same names among different types. 122 09:52.520 --> 09:57.050 Let's go to the game type and do the same thing there. 123 09:57.140 --> 10:00.600 I'm going to change the name just to print. 124 10:00.650 --> 10:05.510 Now we can go back to the main function and use the new method names there. 125 10:05.720 --> 10:16.400 Here I'm going to call the print method instead and another one here let's try it nice. 126 10:16.500 --> 10:24.790 It works on the left the book type has a print method you can run the first print method on the left 127 10:24.880 --> 10:28.240 only through book values on the right side. 128 10:28.270 --> 10:31.430 The game type has a print method as well. 129 10:31.450 --> 10:37.910 However this time you can run the second print method only with game values. 130 10:38.260 --> 10:47.410 So this means that a method belongs to a single type I can use the same method names because they belong 131 10:47.530 --> 10:49.030 to different types. 132 10:49.120 --> 10:52.180 Each type has its own namespace. 133 10:52.360 --> 10:58.090 If those methods were functions I wouldn't be able to use the same names. 134 10:58.120 --> 11:00.420 All right that's all for now. 135 11:00.430 --> 11:02.110 See you in the next lecture.