WEBVTT 0 00:01.390 --> 00:02.850 Welcome back. 1 00:02.890 --> 00:08.760 When you have a lot of conditions to check you can also use a type switch. 2 00:08.890 --> 00:11.540 Let me show you how it works. 3 00:11.560 --> 00:19.150 This is what a type switch looks like it detects and extracts the dynamic value from an interface 4 00:19.150 --> 00:19.840 value. 5 00:19.870 --> 00:27.910 It works just like a type assertion but in a switch statement when you say dot type within parentheses 6 00:28.150 --> 00:31.600 the switch statement becomes a type switch. 7 00:31.810 --> 00:37.450 So you can use it to extract dynamic values from interface values. 8 00:37.450 --> 00:45.520 The V should be an interface value so the type switch can extract the dynamic value from the interface 9 00:46.330 --> 00:54.610 on the left side there is a variable the type switch assigns to extract the value to this variable. 10 00:54.640 --> 00:56.520 This is the interesting part. 11 00:56.740 --> 01:03.210 The type of this variable will change depending on the extracted value. 12 01:03.250 --> 01:08.080 For example let's say you want to extract an int value. 13 01:08.290 --> 01:16.720 In the case statement the variable becomes an int variable or let's say you want to extract a string 14 01:16.780 --> 01:17.250 value. 15 01:17.770 --> 01:21.820 So the variable e becomes a string variable. 16 01:21.820 --> 01:30.040 You can also use a default case if the value in the interface is neither an int or a string. 17 01:30.040 --> 01:33.000 The type switch won't extract anything. 18 01:33.010 --> 01:41.670 Instead it will assign the given interface value to the variable e in summary unlike a regular switch statement 19 01:41.890 --> 01:50.020 type switch compares types rather than values cases match actual types inside the given interface 20 01:50.020 --> 01:50.430 value. 21 01:51.100 --> 01:52.510 Let's take a look at an example. 22 01:52.540 --> 01:58.080 So you can understand it better I was working on the book type. 23 01:58.160 --> 02:00.760 There are a lot of type checkings here. 24 02:00.830 --> 02:03.530 Let's use a type switch instead. 25 02:03.650 --> 02:06.770 First I'm going to type a switch statement. 26 02:06.770 --> 02:10.580 I want to extract the type inside the empty interface. 27 02:10.670 --> 02:14.290 So I'm going to type `v.(type)`. 28 02:14.330 --> 02:20.660 In this case the type switch will only tell us the type inside the interface. 29 02:20.840 --> 02:27.230 But it won't extract the value for now let's use it this way. 30 02:27.230 --> 02:31.340 I'm going to add an int case and a string case. 31 02:31.460 --> 02:34.820 I will also add a default case. 32 02:34.820 --> 02:39.140 Now let's print the type of the value inside each condition 33 02:42.940 --> 02:44.690 let's try it. 34 02:44.710 --> 02:54.160 Moby Dick has an integer publishing date so that int case matches and it prints int Odyssey has a string 35 02:54.250 --> 02:54.850 date. 36 02:54.850 --> 03:01.130 So the string case prints string Hobbit doesn't have a date. 37 03:01.300 --> 03:10.150 So the default case prints nil as you can see the type switch compares types not values. 38 03:10.150 --> 03:14.610 Now let's remove the print functions and use it for real. 39 03:14.740 --> 03:17.020 If the publishing date is nil. 40 03:17.020 --> 03:19.390 Default clause will run. 41 03:19.420 --> 03:20.530 So I'm going to return. 42 03:20.530 --> 03:25.290 Unknown if a date is an int, the int case will run. 43 03:25.810 --> 03:34.180 So I'm going to move the assignment to here if the date is a string. the string case will run. 44 03:34.780 --> 03:38.680 So I'm going to move the conversion to here. 45 03:38.710 --> 03:44.110 Lastly I'm going to move the T variable before the switch. 46 03:44.110 --> 03:48.210 All the cases use it but it doesn't work. 47 03:48.210 --> 03:48.940 Why? 48 03:49.230 --> 03:52.530 Here it says V is not an int. 49 03:52.680 --> 03:56.130 The next error says V is not a string. 50 03:56.700 --> 04:03.480 This happens because I forgot to create a variable to solve this problem. 51 04:03.480 --> 04:07.610 I'm going to declare a variable nice. 52 04:07.650 --> 04:09.630 The errors are gone. 53 04:09.720 --> 04:13.070 V starts as an empty interface. 54 04:13.200 --> 04:18.330 If the int case executes it becomes an int variable. 55 04:18.330 --> 04:23.930 Or if the string case executes it becomes a string variable. 56 04:23.940 --> 04:33.020 However if no case matches in the default case it (`v`) stays the same as an empty interface. Phew!... 57 04:33.040 --> 04:34.850 Now I can run the code. 58 04:34.930 --> 04:35.550 Excellent. 59 04:35.560 --> 04:36.680 It works. 60 04:36.940 --> 04:44.100 Now let's nicely format the dates as you can see the publishing dates are looking weird. 61 04:44.320 --> 04:49.920 I want them to look like this but how can I format them like this? 62 04:49.930 --> 04:52.200 I have a time value. 63 04:52.360 --> 04:55.040 Let's take a look at its methods. 64 04:55.060 --> 04:57.020 I'm looking for the Format method. 65 05:00.280 --> 05:03.490 It converts a time value to a string. 66 05:03.520 --> 05:11.560 Using the given layout. Go uses a rather unusual layout format that looks like this. 67 05:11.620 --> 05:16.750 I want to print a four digit year and a short month name like this. 68 05:17.080 --> 05:23.240 So I need to pick these parts from the reference date and put it into my layout. 69 05:23.290 --> 05:32.470 The reference year is 2006 so I'm going to type 2006 to my layout and the reference month is January. 70 05:33.010 --> 05:38.620 so I'm going to type Jan to my layout instead of calling the string method. 71 05:38.710 --> 05:42.870 I'm going to call the format method with my layout like this. 72 05:42.940 --> 05:44.500 Let's try it. 73 05:44.500 --> 05:54.330 It looks good I can change the layout whatever I want as long as I use the parts of the reference date. 74 05:54.400 --> 05:58.820 Now it prints the dates with a different layout. 75 05:58.960 --> 06:02.840 Cool but I want to print the month as the last. 76 06:03.130 --> 06:08.990 Let's change the layout for the last time okay. 77 06:09.070 --> 06:17.560 It's better in summary if you want to type assert for multiple types it can be better to use a type 78 06:17.590 --> 06:18.940 switch. 79 06:18.940 --> 06:24.500 Otherwise it's perfectly fine to use a simple type assertion. 80 06:24.580 --> 06:26.830 See you in the next lecture.