WEBVTT 0 00:00.620 --> 00:09.170 Up until now you've been using interfaces however you might not want to use them for the sake of using 1 00:09.170 --> 00:17.990 them so in this lecture we're going to simplify the program that we wrote in the last lecture we're 2 00:18.000 --> 00:22.340 going to use concrete types instead of interfaces. 3 00:22.380 --> 00:31.010 It's because it is easier to work with them and then we're going to create a new type called timestamp. 4 00:31.050 --> 00:37.290 It'll help us to make our code simpler by separating the responsibilities. 5 00:37.300 --> 00:41.990 Lastly you're going to discover the power of embedding. 6 00:42.040 --> 00:43.770 Let's take a look at these types. 7 00:43.870 --> 00:50.560 They all are embedding a product and they have the same set of fields and methods. 8 00:50.560 --> 00:55.870 However they don't have custom fields or behaviors on their own. 9 00:56.050 --> 00:59.290 So they bring unnecessary complexity. 10 00:59.560 --> 01:05.560 Only the book has an additional field just because of that field. 11 01:05.660 --> 01:11.250 I need to use an interface so that I can group all these types. 12 01:11.360 --> 01:14.700 Let's simplify the current design here. 13 01:14.840 --> 01:21.700 I combine all the products into one product type in this way. 14 01:21.830 --> 01:27.680 Our code will become much easier to understand and work with. 15 01:27.680 --> 01:29.360 All right let's get started. 16 01:30.740 --> 01:37.670 Like I said I want to combine all these products in a single product type to do that. 17 01:37.670 --> 01:44.540 I'm going to go to the book type and I'm going to move this published field to the product type and 18 01:44.540 --> 01:53.800 then I'm going to move this format function to the product type again I will use this format function 19 01:53.890 --> 01:55.900 to format the publishing date. 20 01:56.220 --> 01:56.870 OK. 21 01:56.980 --> 02:03.540 By the way this published field doesn't sound right to me so I'm going to change it to released. 22 02:03.670 --> 02:07.310 It's more meaningful for every kind of products. 23 02:07.360 --> 02:12.190 Now I need to find a way to print this field to do that. 24 02:12.190 --> 02:20.050 I'm going to add another verb here and I'm going to call the format function with the product released 25 02:20.050 --> 02:27.170 field I also want to simplify the output a little bit so I'm going to delete this verb from here. 26 02:27.250 --> 02:32.800 Okay now it's time to get rid of all of the other product types. 27 02:32.800 --> 02:39.430 So I'm going to select book game puzzle and toy and I'm going to delete them. 28 02:39.440 --> 02:40.950 Bye bye. 29 02:41.050 --> 02:44.110 Let's open up the list type right now. 30 02:44.110 --> 02:47.110 The list is a slice of item values. 31 02:47.110 --> 02:50.420 However there is only one product type right now. 32 02:50.740 --> 02:56.110 So I'm going to delete this and I'm going to make it a slice of products. 33 02:56.220 --> 02:59.040 I'm going to delete this interface type as well. 34 03:00.060 --> 03:02.070 Why did I use a pointer here? 35 03:02.070 --> 03:07.910 It's because the product type is using methods with pointer receivers. 36 03:07.950 --> 03:14.850 That's why I also want to change these I.T. variables to P. short for a product. 37 03:14.850 --> 03:19.740 Now let's go back to the main function and fix these errors first. 38 03:19.980 --> 03:26.970 I'm going to remove all of these products from here and I'm going to move the release date into the 39 03:27.180 --> 03:35.780 product literal I don't want to move this nil release date into the product it's because I don't 40 03:35.780 --> 03:45.160 want to type nil values for every other products here there is an easier way if I add the field names 41 03:45.160 --> 03:54.530 back I won't need to add a released field for the empty release dates so I'm going to add titles and 42 03:54.540 --> 04:03.040 I'm going to add the price fields back here I only need to add the released field name here 43 04:06.040 --> 04:07.970 and here. 44 04:08.150 --> 04:17.190 I don't need the type nil values now because I'm using field names I love concise variable names 45 04:17.370 --> 04:23.370 so I'm going to change the name of the store to l. OK. Showtime! 46 04:23.390 --> 04:24.840 Let's try this one. 47 04:25.530 --> 04:26.050 Great. 48 04:26.100 --> 04:26.920 It works. 49 04:26.940 --> 04:28.320 I want to make it simpler. 50 04:28.320 --> 04:32.670 So I'm going to delete the rest of these products from here. 51 04:32.670 --> 04:33.570 We don't need them. 52 04:33.570 --> 04:36.810 We can only work with these product values. 53 04:36.810 --> 04:37.520 All right. 54 04:37.590 --> 04:40.800 I think the code is much simpler now. 55 04:40.950 --> 04:46.320 There is only one product type which has this one and there is a list of products. 56 04:46.410 --> 04:47.190 That's it. 57 04:47.190 --> 04:49.350 It's very simple. 58 04:49.390 --> 04:50.690 We are not done yet. 59 04:50.690 --> 04:57.230 Let's take a look at the product type it knows how to print a release date. 60 04:57.230 --> 04:59.690 I think it knows too much. 61 04:59.690 --> 05:03.570 Let's move this responsibility to another type. 62 05:03.590 --> 05:10.760 Here it is the timestamp with only one responsibility to format and print timestamps. 63 05:10.760 --> 05:17.960 So the released field will be a timestamp and the product will become much simpler. 64 05:17.960 --> 05:23.810 Let's jump back into the code and let's create a new file timestamp. 65 05:23.840 --> 05:34.660 .go and let's make a new strike type timestamp struck at timestamp is basically a time value. 66 05:34.660 --> 05:36.770 So I'm going to embed the time. 67 05:36.970 --> 05:40.530 This way I can use the methods of the time type. 68 05:40.630 --> 05:46.740 Like I said the timestamp knows how to format and print itself. 69 05:46.870 --> 05:49.080 So let's attach a string method. 70 05:50.170 --> 05:57.420 T S timestamp string which returns a string. 71 05:57.440 --> 06:01.140 Now you're going to see why I am using embedding. 72 06:01.160 --> 06:05.920 For example let's say the timestamp is not set yet. 73 06:05.990 --> 06:08.130 So how can I detect that. 74 06:08.150 --> 06:12.250 Well I can use the zero method of the time. 75 06:12.410 --> 06:16.280 So I'm going to call T S is zero. 76 06:16.310 --> 06:22.300 This method reports whether the time value represents the zero time instant. 77 06:22.520 --> 06:26.010 So I can say that the timestamp is empty. 78 06:26.150 --> 06:28.490 In that case I'm going to return. 79 06:28.490 --> 06:35.630 Unknown otherwise I'm going to format and return the timestamp as a string. 80 06:35.630 --> 06:43.900 So first I'm going to create a time layout and I'm going to format the timestamp using the layout. 81 06:45.310 --> 06:47.260 And I'm going to return it. 82 06:47.380 --> 06:55.850 Now you see why I'm using embedding this way I can use the methods of the time type. 83 06:55.930 --> 06:57.430 That's awesome. 84 06:57.430 --> 06:58.140 All right. 85 06:58.270 --> 07:05.210 The next thing that I want to do is to use this Timestamp Type from the product. 86 07:05.320 --> 07:07.990 So let's go to the product and I'm going to change this 87 07:07.990 --> 07:11.090 this empty interface to timestamp. 88 07:11.200 --> 07:11.930 That's it. 89 07:12.010 --> 07:18.190 But the product is still using the format function to format release dates. 90 07:18.190 --> 07:25.210 So I'm going to delete this format from here and I'm going to call the string method instead. 91 07:25.220 --> 07:30.940 Now the responsibilities are clear the product doesn't care half the format. 92 07:31.120 --> 07:32.510 Release dates. 93 07:32.510 --> 07:33.350 All right. 94 07:33.350 --> 07:35.560 Let's go back to the main function. 95 07:35.570 --> 07:37.450 There are a few problems here. 96 07:37.490 --> 07:44.330 The released field is now at timestamp so it cannot contain arbitrary values like these. 97 07:44.330 --> 07:48.680 So I need to convert them to timestamp values to do that. 98 07:48.680 --> 07:52.720 Let's use a helper function to timestamp. 99 07:52.790 --> 07:56.800 I'm also going to add one more here. 100 07:56.870 --> 07:57.710 That's it. 101 07:57.710 --> 08:00.320 Of course this function doesn't exist yet. 102 08:00.320 --> 08:03.710 So let's go to the product and create it. 103 08:03.830 --> 08:07.080 But I don't have to declare a new function. 104 08:07.130 --> 08:10.820 Instead I'm going to use the format function. 105 08:11.060 --> 08:19.970 I'm just going to change its name to to timestamp it so to return a new timestamp 106 08:19.970 --> 08:20.980 I made it a named (result) value. 107 08:21.020 --> 08:25.670 This way I can use the timestamp's zero value since. 108 08:25.760 --> 08:28.310 Now we are returning a timestamp. 109 08:28.370 --> 08:31.730 I'm going to remove this clause from here. 110 08:31.730 --> 08:35.340 So now I need to return a timestamp value. 111 08:35.510 --> 08:45.030 But now this code converts an integer which is this one into a time value but now I need to return a 112 08:45.030 --> 08:45.630 timestamp. 113 08:46.200 --> 08:53.490 So I'm going to delete this layout from here and I'm going to set this time value to the time field 114 08:53.550 --> 08:54.770 of the timestamp. 115 08:54.960 --> 08:57.450 And I'm going to directly return the timestamp. 116 08:57.870 --> 09:00.260 I don't need to format it. 117 09:00.400 --> 09:05.180 It's because the timestamp already knows how the format and print itself. 118 09:05.550 --> 09:11.310 So here I just need to convert the integer into a timestamp. 119 09:11.400 --> 09:12.300 That's it. 120 09:12.300 --> 09:20.600 By the way let's also move this method from here and save this into the timestamp. 121 09:20.610 --> 09:27.380 This is a better place for this function all the timestamp related things are happening here. 122 09:27.570 --> 09:34.260 And if I run this you can see that Moby Dick and Odyssey get a release date. 123 09:34.320 --> 09:42.090 So the timestamp formats these release dates because the Moby Dick has an integer release date and Odyssay 124 09:42.090 --> 09:45.090 has a string release date. 125 09:45.090 --> 09:54.830 So in this switch statement they get converted into integer values but for the hobbit it says unknown. 126 09:54.870 --> 10:01.560 So it doesn't match anything in here and it gets a zero time value from the beginning and then we set 127 10:01.560 --> 10:07.130 it into the timestamp and return the timestamp so the timestamp becomes zero. 128 10:07.140 --> 10:14.370 So when we want to print it with the string method so the if statement here catches that it's a 129 10:14.370 --> 10:19.880 zero timestamp then it returns unknown. 130 10:19.890 --> 10:21.720 All right let's summarize. 131 10:22.080 --> 10:26.450 We have two types product and timestamp. 132 10:26.520 --> 10:31.710 Let's say I want to create a product value with a particular release date. 133 10:31.770 --> 10:36.530 But the released field is a timestamp to get a timestamp value. 134 10:36.570 --> 10:44.070 I call the to timestamp function then it returns me a timestamp value. 135 10:44.070 --> 10:48.970 Now I can put that value to the released field of the product. 136 10:48.990 --> 10:52.800 Let's say I want to print the product to do that. 137 10:52.800 --> 11:00.390 I just need to call the print method but the product doesn't know how to print timestamp values. 138 11:00.650 --> 11:03.140 Only the timestamp knows how to do that. 139 11:03.500 --> 11:12.290 So it calls the timestamp's string method and asks the timestamp to print itself in the end the product 140 11:12.590 --> 11:15.220 prints itself and the release date. 141 11:15.380 --> 11:18.710 The product was formatting and printing. 142 11:18.710 --> 11:21.200 The release dates by itself. 143 11:21.200 --> 11:22.910 Now it is much simpler. 144 11:23.120 --> 11:29.400 The released field knows of the format and print itself. 145 11:29.440 --> 11:30.190 All right. 146 11:30.190 --> 11:31.810 That's all for now. 147 11:31.930 --> 11:36.120 You may pause the video now and take a look at the lecture summary.