WEBVTT 0 00:01.270 --> 00:04.630 Empty interface says nothing. 1 00:04.630 --> 00:07.510 So what is an empty interface? 2 00:07.510 --> 00:10.030 Well this is an empty interface. 3 00:10.030 --> 00:10.940 I'm not kidding. 4 00:11.020 --> 00:12.600 It doesn't have a method. 5 00:12.760 --> 00:14.860 It is completely empty. 6 00:14.860 --> 00:16.980 It says nothing. 7 00:17.110 --> 00:20.870 You don't need to satisfy it because it has no methods. 8 00:20.890 --> 00:26.460 Every type can satisfy it so it can represent any type of value. 9 00:26.670 --> 00:29.990 You can type it more concisely in a single line. 10 00:30.060 --> 00:32.520 We usually use it like this. 11 00:32.520 --> 00:33.970 This is the same thing. 12 00:33.990 --> 00:39.340 It's still an empty interface type. 13 00:39.380 --> 00:41.770 This is an empty interface value. 14 00:41.840 --> 00:47.540 You can declare it like so this variable holds an empty interface value. 15 00:47.960 --> 00:51.350 Let's say there is a Tetris value in it. 16 00:51.410 --> 00:55.580 The Tetris variable has two methods that you can use. 17 00:55.670 --> 01:01.970 Let's put it in the empty interface value. 18 01:02.170 --> 01:05.470 You can no longer use the methods of the game. 19 01:05.770 --> 01:13.450 The empty interface value wraps it in a hidden place so the empty interface says nothing. 20 01:13.450 --> 01:18.870 Let's take a look at an example. 21 01:18.950 --> 01:26.150 First I'm going to create a new directory for the next example with new main.go file. 22 01:26.150 --> 01:35.690 Next I'm going to declare an empty interface variable let's assign it a slice Let's print it. 23 01:35.830 --> 01:36.630 No problem. 24 01:36.640 --> 01:38.410 It can store a slice. 25 01:38.410 --> 01:42.780 Now let's assign it a map. Again, 26 01:42.840 --> 01:43.770 no problem. 27 01:43.770 --> 01:48.370 Let's assign it's a string value. No problem at all. 28 01:48.370 --> 01:51.480 Lastly let's assign it a number. 29 01:51.490 --> 01:58.030 There are no errors because you can store any type of value in an empty interface value. 30 01:58.030 --> 02:04.690 However you cannot directly use an empty interface value in operations. 31 02:04.720 --> 02:09.350 For example let's try to multiply it with a number. 32 02:09.370 --> 02:13.440 It didn't work because the any variable is not a number. 33 02:13.600 --> 02:16.160 It's an empty interface value. 34 02:16.210 --> 02:24.370 Remember an interface value has two parts a dynamic value and a dynamic type. 35 02:24.400 --> 02:32.910 So first you need to extract the dynamic value from the interface value using the dynamic type here. 36 02:32.980 --> 02:42.220 The dynamic type is int because lastly I assigned it an integer so let's use an assertion to extract 37 02:42.310 --> 02:42.880 the number 38 02:45.880 --> 02:46.350 nice. 39 02:46.420 --> 02:46.980 It works. 40 02:46.990 --> 02:54.160 Now as you can see it doesn't matter what kind of value you store in an interface value. 41 02:54.400 --> 02:58.900 However you want to use the dynamic value in operations. 42 02:58.900 --> 03:03.160 You must first extract it from the interface value here. 43 03:03.250 --> 03:05.710 The dynamic type is an integer. 44 03:05.710 --> 03:10.470 So first I get it from the interface value as an integer. 45 03:10.540 --> 03:13.140 Only then I can multiply it. 46 03:13.150 --> 03:16.440 Now I'm going to create another directory with a new file 47 03:19.370 --> 03:27.010 let's create an int slice let's create an empty interface variable. 48 03:27.010 --> 03:31.090 I'm going to assign it the integer slice as you can see. 49 03:31.210 --> 03:35.950 You can also assign a slice to an empty interface value. 50 03:35.950 --> 03:43.060 However as I said you cannot directly use the dynamic value of an interface value. 51 03:43.060 --> 03:46.640 It means you cannot use it here as a slice. 52 03:46.720 --> 03:54.870 For example let's try to find the length of the slice using the empty interface variable. 53 03:54.890 --> 04:00.800 Now I cannot get its length because any is an interface type. 54 04:00.800 --> 04:04.830 It's not a slice type so it has no length. 55 04:05.000 --> 04:08.130 However it stores an int slice. 56 04:08.480 --> 04:12.360 So first I need to get the slice back from it. 57 04:12.380 --> 04:14.750 Now I can get its length. 58 04:15.050 --> 04:20.960 The last statement works because I'm asserting that it's an int slice. 59 04:21.440 --> 04:29.890 If the dynamic type inside the interface was not an int slice my program would crash in the runtime. 60 04:29.900 --> 04:35.090 This time I'm going to create a slice of empty interface values. 61 04:35.150 --> 04:38.960 Why do you think? Can I assign it the nums slice? 62 04:39.320 --> 04:40.840 It didn't work. 63 04:40.880 --> 04:48.190 The error says you cannot assign an int slice to an empty interface slice. 64 04:48.210 --> 04:57.480 It's like trying to assign the nums slice to a string slice it is the same error a type mismatch. 65 04:57.480 --> 04:59.720 So what is the solution? 66 04:59.730 --> 05:06.190 Well I can use a loop then I need to add the next number. 67 05:06.450 --> 05:11.180 Let's print the result it works. 68 05:11.250 --> 05:18.520 But why? I need to do so because the empty interface slice is not an empty interface value. 69 05:18.690 --> 05:21.540 It's an empty interface slice. 70 05:21.540 --> 05:28.490 That's why I need to add the numbers manually one by one to the empty interface slice. 71 05:28.500 --> 05:31.980 Let's talk about this a little bit more. 72 05:32.100 --> 05:42.080 The empty interface slice consists of empty interface values. The int slice consists of integer values. 73 05:42.230 --> 05:47.450 The empty interface slice is a slice of empty interface values. 74 05:47.450 --> 05:54.980 But it's not an empty interface value so you cannot directly assign it another type of a slice. 75 05:55.610 --> 06:01.510 However, you can assign any type of value to one of its elements. 76 06:01.670 --> 06:07.010 It's because each one of its elements is an empty interface value. 77 06:07.100 --> 06:08.770 OK enough theory. 78 06:09.020 --> 06:12.130 Let's take a look at a practical example. 79 06:12.260 --> 06:16.430 First of all let me refactor this code a little bit. 80 06:16.430 --> 06:21.070 I'm going to put all the variables directly into the store. 81 06:21.130 --> 06:23.980 Now I can remove the store variable. 82 06:24.400 --> 06:25.900 Let's go to the book type. 83 06:26.500 --> 06:31.540 Let's say you need to store the publishing dates of the books to do that. 84 06:31.540 --> 06:35.480 I'm going to add a field with an empty interface. 85 06:35.590 --> 06:42.310 I'm going to tell you why in the main function first I'm going to add two more books. 86 06:42.440 --> 06:45.830 The first book uses an integer. 87 06:45.830 --> 06:48.830 The second book uses a string. 88 06:48.890 --> 06:52.220 The third book doesn't have a date at all. 89 06:52.220 --> 06:54.780 These are unix timestamps. 90 06:54.850 --> 06:59.610 You will see how you can convert them to actual dates in a minute. 91 06:59.720 --> 07:04.210 As you can see each book has a different type of publishing date. 92 07:04.220 --> 07:11.090 That's why I have declared the published field as an empty interface so I can store different type of 93 07:11.090 --> 07:12.900 values in it. 94 07:12.950 --> 07:15.500 Let's go back to the book type here. 95 07:15.620 --> 07:21.360 I'm going to print the published date. 96 07:21.520 --> 07:23.140 I'm going to run it. 97 07:23.140 --> 07:24.750 It doesn't look good. 98 07:24.760 --> 07:27.250 I'm going to make its prey to do that. 99 07:27.250 --> 07:29.350 I'm going to use a function. 100 07:29.350 --> 07:34.500 It will take an empty interface value and return back a string. 101 07:34.540 --> 07:44.460 I'm going to use it in the printf function. OK. Let's create the format function it takes an empty interface 102 07:44.910 --> 07:47.010 and it returns a string. 103 07:47.010 --> 07:49.710 For now I will simply return a string. 104 07:52.390 --> 07:53.070 okay. 105 07:53.190 --> 07:54.270 It works. 106 07:54.570 --> 07:58.350 Let's format the dates now. As a reference, 107 07:58.350 --> 08:00.220 Let's copy the book values, here. 108 08:00.300 --> 08:00.630 Let's copy the book values, here. 109 08:04.260 --> 08:07.400 okay the last book has no date. 110 08:07.410 --> 08:10.440 So first let's check for it. 111 08:10.560 --> 08:14.880 The zero value of an interface value is nil. 112 08:14.880 --> 08:20.310 So if the publishing date is missing, it will be a nil value. 113 08:20.370 --> 08:31.160 Only then I will say it's unknown. Otherwise, I will return an empty string. Nice. Let's format the other 114 08:31.160 --> 08:33.020 dates as well. 115 08:33.020 --> 08:35.810 I'm going to start with Moby Dick. 116 08:35.840 --> 08:45.140 It uses an integer date so I'm going to declare an int variable and I'm going to extract the int value 117 08:45.230 --> 08:47.930 using a type assertion. 118 08:47.930 --> 08:51.980 If it succeeds the variable will be an integer. 119 08:51.980 --> 08:55.680 So I'm going to assign to the T variable. 120 08:55.700 --> 08:59.870 Now it's time to convert the integer to a time value. 121 09:00.050 --> 09:09.140 To do that I'm going to call the time Unix function its last parameter is not important but it doesn't 122 09:09.140 --> 09:15.290 work because the Unix function takes an int64 value. 123 09:15.440 --> 09:19.070 So I need to convert the variable like so. 124 09:19.490 --> 09:21.830 Now I've got a time value. 125 09:21.830 --> 09:28.120 It has a string method I can use it to convert the time value to a string. 126 09:28.130 --> 09:29.930 Now I can return it as well. 127 09:31.520 --> 09:34.250 Okay let's try it. 128 09:34.340 --> 09:39.090 It doesn't look good but it prints a more readable date. 129 09:39.170 --> 09:45.330 Now let's format the last book it uses a string date. 130 09:45.340 --> 09:50.190 So I'm going to extract the string value if it succeeds. 131 09:50.200 --> 09:52.320 The variable will be a string. 132 09:52.780 --> 09:58.640 So I need to convert it to an integer. 133 09:58.780 --> 09:59.280 Good. 134 09:59.290 --> 10:02.660 It works in summary. 135 10:02.660 --> 10:06.390 Go allows you to write type safe code. 136 10:06.470 --> 10:10.990 However the empty interface can cross that line. 137 10:11.030 --> 10:16.280 Your programs can become brittle and hard to maintain. 138 10:16.280 --> 10:21.470 Do not use the empty interface unless really necessary. 139 10:21.470 --> 10:22.970 See you in the next lecture.