WEBVTT 0 00:00.880 --> 00:01.720 Welcome back. 1 00:01.990 --> 00:11.200 Let me tell you an essential knowledge unlike some other Object-Oriented-Programming languages Go does not have classes the 2 00:11.200 --> 00:15.640 methods can be attached to almost any type. 3 00:15.640 --> 00:18.550 It doesn't have to be a struct type. 4 00:18.550 --> 00:27.610 For example you can attach methods to the basic types such as an integer string or float or you can 5 00:27.670 --> 00:37.510 attach them to bare types like arrays or structs or you can attach them to pointer bearing types like slices 6 00:37.600 --> 00:39.590 maps or channels. 7 00:39.790 --> 00:43.760 You can even attach methods to functions. 8 00:43.780 --> 00:52.030 This last one may sound kind of weird but in some situations, it can become very useful for the types 9 00:52.030 --> 00:53.570 on the right side. 10 00:53.590 --> 01:03.240 Don't use pointer receivers with them as you know they already carry a pointer with themselves OK. 11 01:03.310 --> 01:05.590 Let's talk about the example. 12 01:05.590 --> 01:06.880 I'm going to show you. 13 01:07.240 --> 01:15.660 Initially there will be a simple slice without any method in the end it will have a printing method 14 01:15.720 --> 01:19.480 that can print all the games in the slice. 15 01:19.590 --> 01:21.500 OK let's get started. 16 01:21.500 --> 01:28.170 First of all I'm going to remove everything but the variables from here since I've got a few games to 17 01:28.170 --> 01:36.000 sell let's create a slice to hold all of them together each element of the slice is a pointer to a game 18 01:36.000 --> 01:37.140 value. 19 01:37.140 --> 01:40.440 I'm going to add Minecraft and Tetris to the slice 20 01:42.980 --> 01:45.900 since this is a slice of game pointers. 21 01:46.010 --> 01:49.700 I cannot add a book value to it because of that. 22 01:49.790 --> 01:54.180 I'm going to comment out the Moby Dick which is a book value. 23 01:54.320 --> 02:01.930 Now I'm going to print all the elements using a loop and I'm going to call the print method of the next 24 02:01.930 --> 02:03.440 game value. 25 02:03.580 --> 02:04.090 That's it. 26 02:04.390 --> 02:07.070 Let's try it nice. 27 02:07.090 --> 02:09.340 It lists the games. 28 02:09.340 --> 02:11.890 Let's organize the code a little bit. 29 02:11.890 --> 02:15.340 I'm going to create a new file list. 30 02:15.390 --> 02:19.410 Now I'm going to create a method to list all the games. 31 02:19.410 --> 02:22.020 Let's copy the code from the main function 32 02:24.720 --> 02:30.260 this method receives a game slice using the L receiver variable. 33 02:30.510 --> 02:36.630 So I just need to change the items variable like this. Oops! 34 02:36.670 --> 02:38.170 There is an error. 35 02:38.170 --> 02:41.980 It says that the receiver type is invalid. 36 02:42.170 --> 02:46.260 It's because the game slice is not a defined type. 37 02:46.390 --> 02:48.090 It's an unnamed type. 38 02:48.550 --> 02:55.900 So it doesn't belong to any package a type and its methods should be in the same package. 39 02:55.900 --> 02:58.680 That's why I need to create a new type. 40 02:58.690 --> 03:03.470 So first I need to define a new type in the current package. 41 03:03.550 --> 03:08.230 To do that I'm going to declare a new type using the game slice type. 42 03:08.560 --> 03:11.320 Now we've got a new type with a new name. 43 03:11.320 --> 03:14.820 I need to attach the print method to the list type. 44 03:15.550 --> 03:20.180 So I'm going to change the receiver type to the list type. 45 03:20.250 --> 03:23.080 Let's get back to the main function. 46 03:23.130 --> 03:26.240 How can I use the list type here? 47 03:26.340 --> 03:35.160 First I'm going to create a new variable and I'm going to convert the item slice to the list type I 48 03:35.160 --> 03:40.940 can do so because the underlying type of the list type is a game slice. 49 03:41.190 --> 03:49.020 Both the list type and the items slice have the same underlying type thanks to the list type the variable 50 03:49.020 --> 03:51.880 my has got a print method on the fly. 51 03:52.140 --> 03:52.950 Let's use it. 52 03:53.340 --> 03:56.390 OK let's save and try it. 53 03:56.580 --> 03:57.310 It works. 54 03:57.420 --> 04:07.030 Excellent here I add the methods of the list type to the items slice on the fly I just needed to convert 55 04:07.120 --> 04:13.060 the slice to the list type imagine how powerful this can be. 56 04:13.070 --> 04:18.170 This means that you don't have to add methods to a type right from the start. 57 04:18.380 --> 04:22.600 Using this trick you can always add methods later on. 58 04:23.350 --> 04:31.950 OK let's take a look at another example let's say there are no games in the store so I'm going to set 59 04:31.950 --> 04:41.070 the variable my to nil it runs without any problems but it doesn't print anything. 60 04:41.350 --> 04:43.240 Let's make it prettier. 61 04:43.320 --> 04:47.620 The underlying type of the list type is a slice. 62 04:47.620 --> 04:49.900 So I can use it as a slice. 63 04:50.020 --> 04:56.530 For example I can check whether the receiver is empty or or not. If so, 64 04:56.590 --> 04:59.680 I'm going to print a message and return. 65 04:59.680 --> 05:01.210 Let's try it. 66 05:01.210 --> 05:04.050 Now it says that the store is closed. 67 05:04.060 --> 05:06.320 Nice here. 68 05:06.370 --> 05:14.170 The variable my is nil but I could run the print method on it so you can call methods on a nil value 69 05:14.230 --> 05:15.100 as well. 70 05:15.100 --> 05:19.030 For now let's comment out the nil assignment. 71 05:19.030 --> 05:23.940 As I said you can attach methods to almost any type. 72 05:24.040 --> 05:29.700 As an example I will create a new float type which I call money. 73 05:29.860 --> 05:35.400 It will format the desired amount of dollars when I run the method. 74 05:35.410 --> 05:39.030 It will return a String value like this. 75 05:39.310 --> 05:44.170 I will use the money type for the game and book types. 76 05:44.290 --> 05:50.110 Okay let's get back to the coding editor. Let's create the money type in a new file. 77 05:52.440 --> 05:56.640 I'm going to declare a new type using the float type. 78 05:56.640 --> 06:02.430 Remember a type and its methods should be in the same package. 79 06:02.460 --> 06:06.460 That is why I needed to create a new type. 80 06:06.500 --> 06:08.430 Now I have a money type. 81 06:08.570 --> 06:20.210 Let's add a method to it let's name it string it returns a string let's use the sprintf function to 82 06:20.210 --> 06:28.280 format and return the dollar amount I will use the money type for the price fields of game and book 83 06:28.280 --> 06:29.570 types. 84 06:29.570 --> 06:34.490 OK let's go to the game type and use this new type there. 85 06:34.740 --> 06:42.590 First I'm going to change the price field to money to return the price I need to run the string method 86 06:42.680 --> 06:44.840 on the price field. 87 06:44.840 --> 06:53.450 I can do so because now the price field is a money type and the money type has a string method since 88 06:53.510 --> 06:55.980 it returns a string. 89 06:56.060 --> 06:58.810 I'm going to change the float verb to a string. 90 06:58.820 --> 07:03.740 Think of course there is an error in the discount method. 91 07:03.830 --> 07:09.930 The price field is a money but the ratio argument is float. 92 07:10.160 --> 07:19.680 So the types don't match I can solve this problem by converting the expression on the right to the money 93 07:19.680 --> 07:21.640 type okay. 94 07:21.680 --> 07:22.400 Nice. 95 07:22.490 --> 07:25.230 Let's do the same for the book. 96 07:25.370 --> 07:34.230 I'll change the price field first and I'm going to call the string method. 97 07:34.250 --> 07:35.150 That's it. 98 07:35.150 --> 07:36.710 Let's try it. 99 07:36.710 --> 07:37.430 Nice. 100 07:37.430 --> 07:42.470 Now the price fields are printing themselves as you can see. 101 07:42.500 --> 07:49.290 You can also attach methods to basic types congrats. 102 07:49.290 --> 07:51.290 This was the methods. 103 07:51.390 --> 07:52.800 See you in the next lecture.