WEBVTT 0 00:00.150 --> 00:04.230 How can you make a type print itself automatically. 1 00:04.230 --> 00:06.960 Let me explain what I mean. 2 00:06.960 --> 00:09.040 Let me show you a quick example. 3 00:09.060 --> 00:15.010 Now I am on the list type and if I go and run this code it prints the products. 4 00:15.240 --> 00:21.520 But what if I want to print the products using the plain old printer function. 5 00:21.570 --> 00:31.770 So I'm going to add a printf here and print each product using its value so if I go and run this again 6 00:32.130 --> 00:33.120 What's this. 7 00:33.120 --> 00:36.610 These are the fields data of the product. 8 00:36.630 --> 00:39.060 Each one of these is a product value. 9 00:39.060 --> 00:44.270 So how can I print a product without calling its print method. 10 00:44.280 --> 00:47.730 Let's go ahead and talk about this. 11 00:47.780 --> 00:54.200 We're going to use an interface called Stringer to print a product automatically as a string. 12 00:54.200 --> 01:00.950 This interface customizes the way a type represents itself as a string. 13 01:01.160 --> 01:08.630 All of the printing functions in the fmt package can automatically call the string method. 14 01:08.630 --> 01:16.850 In other languages there are also similar ways to print a type as a string the stringer interface is 15 01:16.850 --> 01:19.850 not the only famous interface. 16 01:19.850 --> 01:24.830 There are a lot of other well-known interfaces in go standard library. 17 01:24.830 --> 01:27.890 We're going to talk about them in the later lectures. 18 01:27.890 --> 01:29.150 No worries. 19 01:29.150 --> 01:29.750 All right. 20 01:29.750 --> 01:35.910 Let's talk about how we can implement the stringer interface for the product type here. 21 01:35.940 --> 01:38.430 We were printing it using print. 22 01:38.880 --> 01:44.520 And it was printing some weird output here. 23 01:44.520 --> 01:47.750 So I'm going to go ahead to the product type here. 24 01:47.850 --> 01:52.410 I just need to change the name of this method to a string. 25 01:52.410 --> 01:56.370 It needs to return a String as well here. 26 01:56.410 --> 01:59.510 It prints itself instead of this. 27 01:59.620 --> 02:02.390 I'm going to call the sprinf function. 28 02:02.410 --> 02:11.860 Sorry and I'm going to return a String value from Sprintf. By the way the list already prints a new 29 02:11.860 --> 02:13.180 line here. 30 02:13.180 --> 02:17.000 So I'm going to remove this new line from here. 31 02:17.020 --> 02:19.670 All right let's try it again. 32 02:19.720 --> 02:20.310 Awesome. 33 02:20.380 --> 02:24.970 Right now we can print product values directly. 34 02:25.060 --> 02:32.650 We don't have to call print method it's because print f knows that the product has a string method and 35 02:32.650 --> 02:35.950 calls this method automatically. 36 02:36.190 --> 02:39.430 So it can print the product automatically. 37 02:39.430 --> 02:46.580 So of course we're not going to stop here and we're going to implement the string method the string 38 02:46.580 --> 02:50.310 or interface in our other types as well. 39 02:50.470 --> 02:53.710 You know this price field is a money type. 40 02:53.710 --> 02:59.190 And here I'm calling the string method to print a money value. 41 02:59.470 --> 03:02.840 Also in here the released field is a timestamp. 42 03:02.950 --> 03:05.800 And I also need to call the string method here. 43 03:06.460 --> 03:14.710 So let me remove these string methods and just to show you what happens when I do so let's save this 44 03:14.710 --> 03:22.580 file and let's retry it as you can see the money type cannot print itself as well as the timestamp 45 03:22.630 --> 03:22.990 type. 46 03:23.840 --> 03:30.660 So I'm going to go to the money type and make it a stringer it's easy here. 47 03:30.670 --> 03:35.420 It's because it already has a string method that returns a string. 48 03:35.420 --> 03:43.520 I just need to change the first letter to s uppercase as you know interfaces in go are case sensitive. 49 03:43.520 --> 03:45.760 That's why I need to do so. 50 03:45.800 --> 03:50.060 So if I go back and run this again you can see that 51 03:50.270 --> 03:52.930 Now the money type can print itself. 52 03:53.150 --> 03:57.110 So let's do the same thing for the timestamp type. 53 03:57.260 --> 04:01.660 I'm going to go to the time stamp and I'm going to change the first letter to s. 54 04:01.700 --> 04:03.240 Just like I did in the money type. 55 04:03.740 --> 04:04.090 All right. 56 04:04.340 --> 04:10.760 So if I go to the terminal and run this code again you can see that the timestamp can print itself. 57 04:11.360 --> 04:12.650 Awesome right. 58 04:12.650 --> 04:13.180 Nice. 59 04:13.190 --> 04:17.310 The money and timestamp types are now stringers. 60 04:17.360 --> 04:19.760 They know how to print themselves. 61 04:19.760 --> 04:21.170 What's left. 62 04:21.170 --> 04:24.800 Actually there is also a list type you know. 63 04:25.070 --> 04:29.810 And I want to make this list type also as Stringer. 64 04:29.840 --> 04:31.590 So how can I do that first. 65 04:31.610 --> 04:38.150 I'm going to change the method name to string and it's going to return a String value of course. 66 04:38.150 --> 04:41.260 And here it prints a message. 67 04:41.300 --> 04:43.690 But I want to return a string here. 68 04:43.730 --> 04:44.970 So I'm going to return a String. 69 04:44.990 --> 04:47.840 directly in here also. 70 04:47.860 --> 04:50.480 I'm going to add a new line character here. 71 04:50.620 --> 04:58.690 Now here I need to return these product values as a single string value. 72 04:58.690 --> 05:03.620 To do that I'm going to use a string builder. 73 05:03.640 --> 05:06.700 This is a buffer for string values. 74 05:06.790 --> 05:14.220 Actually I could have combined these string values myself but that will be very inefficient if I'm going 75 05:14.220 --> 05:17.290 to have multiple string values in the future. 76 05:17.380 --> 05:19.420 So it is better to be ready for it. 77 05:19.420 --> 05:21.220 That's why I'm using the string. 78 05:21.250 --> 05:22.880 builder type here. 79 05:22.890 --> 05:27.100 Now I need to write into the buffer using this string value. 80 05:27.130 --> 05:28.690 So I'm going to call string. 81 05:28.810 --> 05:30.820 I'm going to call the write string method. 82 05:31.210 --> 05:31.900 It's here. 83 05:31.900 --> 05:32.460 Right. 84 05:32.470 --> 05:36.530 It takes a string value but I don't care what it returns. 85 05:36.670 --> 05:43.150 So I'm just going to call the write string method and I'm going to pass it this string value. 86 05:43.300 --> 05:46.810 Next I need to print the product value. 87 05:46.810 --> 05:55.120 So I'm going to call the write string method again and I'm going to pass it in a product value but it 88 05:55.120 --> 05:59.290 doesn't work the product value is not a string value. 89 05:59.380 --> 06:02.040 So I just need to call the string method here. 90 06:02.110 --> 06:09.250 As you can see not every other type knows about the stringer interface so the string builder doesn't 91 06:09.250 --> 06:11.440 know anything about the stringer interface. 92 06:11.440 --> 06:15.480 So I just needed to call the string method here myself. 93 06:15.610 --> 06:22.250 And next I'm going to call the write rune method this time as you can see. 94 06:22.280 --> 06:25.130 This time it takes a rune value. 95 06:25.130 --> 06:31.760 So I'm doing this because I want to print a new line just like in here. 96 06:31.760 --> 06:35.190 So that's why I called this write rune method. 97 06:35.600 --> 06:39.940 So I'm going to pass it in a new line and that's it. 98 06:39.950 --> 06:43.650 Now I can delete these lines of code. 99 06:43.700 --> 06:51.580 All right finally I need to return a single string value so I'm going to say return and I'm going to 100 06:51.580 --> 06:54.320 say string here. 101 06:54.350 --> 06:59.270 I need to call the string method because it returns a string value. 102 06:59.570 --> 07:03.740 So you see the strings builder is also a stringer. 103 07:03.830 --> 07:05.030 So if you need too. 104 07:05.060 --> 07:10.040 You can also pass this builder to printing functions directly. 105 07:10.040 --> 07:15.690 There is only one problem left and it's in the main function here. 106 07:15.770 --> 07:20.300 It tries to call the print method but it doesn't exist anymore. 107 07:20.330 --> 07:25.990 So instead I'm going to print the list directly like this. 108 07:26.120 --> 07:31.660 So if I go back to my terminal and run this code it works as before. 109 07:31.700 --> 07:34.320 Nice isn't it. 110 07:34.330 --> 07:38.860 So how does the stringer work behind the scenes. 111 07:38.890 --> 07:40.450 Let me show you an example. 112 07:40.510 --> 07:50.170 I'm going to comment out this code and I'm going to declare a new money variable pocket money. 113 07:50.320 --> 07:52.000 With $10. 114 07:52.140 --> 07:58.190 So I'm going to print I have pocket. 115 07:58.200 --> 07:58.910 Okay. 116 07:59.100 --> 08:03.740 Now if I go to my terminal and run this code you can see that I'm not that rich. 117 08:03.740 --> 08:05.810 And I have only ten dollars. 118 08:05.810 --> 08:14.100 So how does the println function can print this money type this money value. 119 08:14.140 --> 08:16.300 Let's see how it works. 120 08:16.390 --> 08:25.020 First the program executes the print with a money value the money type has a string method the print 121 08:25.020 --> 08:33.900 function uses a type assertion and sees that the money is a stringer therefore it executes the string 122 08:33.930 --> 08:40.040 method of the money the string method of the money returns a string value. 123 08:40.040 --> 08:44.810 Finally the print function prints ten dollars. 124 08:44.810 --> 08:50.480 So basically it uses a type assertion behind the scenes. 125 08:50.510 --> 08:59.200 This is how it works so previously you learned how to use interfaces to group types by behavior. 126 09:00.220 --> 09:05.470 You learned that you don't always have to create interfaces by yourself. 127 09:05.470 --> 09:15.330 Instead you can satisfy existing interfaces existing interfaces can become bridges between your types 128 09:15.480 --> 09:24.620 and code that is written by others the most significant benefit of using common interfaces is reusability 129 09:25.370 --> 09:32.450 therefore most Go programmers prefer to implement methods of existing interfaces. 130 09:32.450 --> 09:33.700 That's all for now. 131 09:33.770 --> 09:37.280 You may pause the video here and take a look at the summary.