WEBVTT 00:01.110 --> 00:04.560 In this venue we will learn how to deal with failures. 00:04.560 --> 00:07.310 This is handling in a meeting at our values. 00:07.320 --> 00:11.140 Finally we will learn some common idioms and treat errors. 00:11.400 --> 00:12.170 Things failed. 00:12.480 --> 00:13.730 They always do. 00:13.890 --> 00:15.900 And software is no exception. 00:16.230 --> 00:23.720 To deal with this situation go provides an error type specifically designed to handle these cases to 00:23.730 --> 00:24.780 create an error. 00:24.840 --> 00:26.730 We can use the error package. 00:26.790 --> 00:29.890 So let's take a look at the documentation. 00:29.910 --> 00:31.540 This negative contagion of the package. 00:31.600 --> 00:35.630 And as you can see it only exposes one function. 00:35.640 --> 00:39.990 I think this the probably most simple package of the standard library. 00:40.380 --> 00:49.710 So the only function that expose it is new which receives an arguments text of string type and it returns 00:49.890 --> 00:51.120 an error. 00:52.170 --> 00:56.860 So this broadly the theme that we're all gonna be using for these examples. 00:56.850 --> 01:05.520 Remember if you want to change segmentation go to Golden dot org slash EKG slash error to review it 01:05.960 --> 01:08.200 or to get more information about it. 01:09.650 --> 01:19.300 So to start I'd like to go back to the example we saw in the previous video which were about math of 01:19.370 --> 01:27.530 instructors and I would like to expand this by handling this case correctly. 01:27.680 --> 01:32.570 The case where the role is not developer or not architect. 01:32.570 --> 01:38.750 So right now what we're doing is we're just returning zero but it really makes sense to return an error 01:39.050 --> 01:45.780 saying a the road you specified is not percent or I don't know how to handle this. 01:45.980 --> 01:52.280 So let's change these by instead of returning just an integer. 01:52.280 --> 01:57.180 Remember that go functions can return more than one volume so let's return error here. 01:57.260 --> 01:57.620 Right. 01:58.130 --> 02:06.650 So if we do this this should fail because we're just returning 1 value here which is a hundred but it 02:06.650 --> 02:09.790 should be an integer and an error. 02:10.370 --> 02:15.830 But there is no error here so let's just return nil which means nothing. 02:15.830 --> 02:26.000 So as you can see this line is fine now then let's return Neil here because it's also valid. 02:26.040 --> 02:30.370 There's no error here but here in this thing the error case. 02:30.390 --> 02:34.390 So let's return to zero but let's also return an error. 02:34.440 --> 02:35.960 So how do we do this. 02:36.240 --> 02:43.110 We first need to import the error package which is here we are ready so how do we import packages. 02:43.110 --> 02:57.320 And now let's create an error and let's say go or let's say not a will handle this role. 02:57.970 --> 02:58.300 OK. 02:58.690 --> 02:59.950 So now I'm returning an error. 03:00.160 --> 03:01.100 Let's say this. 03:01.790 --> 03:04.420 And the compiler says that everything is fine. 03:04.660 --> 03:07.720 So let's create an ether. 03:07.900 --> 03:20.650 We already saw how to it and let's say that name is Olga and the role will be designer. 03:20.650 --> 03:20.980 Right. 03:21.820 --> 03:28.330 So the salary function doesn't know how to handle the role the designer. 03:29.050 --> 03:36.110 So let's try to execute using the salary and now it's returning to these values. 03:36.440 --> 03:39.980 Let's get the salary here and the error here. 03:40.070 --> 03:48.810 We usually use the error word E R to handle errors variables. 03:48.840 --> 03:52.950 So the type is error and usually this is an idiom. 03:52.950 --> 03:58.830 This is not like it doesn't has to be like this that it's a common pattern go program that you will 03:58.830 --> 04:11.300 see error 2 refers to error variable and now let's see if error is not Neil which means either there 04:11.300 --> 04:23.370 was an error ban equals means different from so let's pretend otherwise as we did before we can print 04:23.690 --> 04:26.850 salary salary. 04:27.180 --> 04:31.680 OK now let's execute this OK. 04:31.740 --> 04:35.550 I'm not humbled to I'm not able to handle this role right now. 04:35.550 --> 04:39.300 If you take a look here the argument of error is new. 04:40.080 --> 04:41.060 It's a string. 04:41.610 --> 04:46.920 So what happened is I would like to be more precise about this error and bring the role that you're 04:46.920 --> 04:49.410 sending to me in the message. 04:49.440 --> 04:49.940 Right. 04:50.700 --> 04:54.590 So what I can do is to use a nested function. 04:54.690 --> 05:03.320 I'm gonna use f empty aspirin F this is very interesting how it works and I'm gonna say the same I'm 05:03.320 --> 05:11.430 not able to handle the person that's s role and what does this mean. 05:11.430 --> 05:21.030 This is going to interpolate variables so I have the role here and I'm saying in these simple person 05:21.040 --> 05:26.690 touch est embeds the variable that I'm passing you are the first argument. 05:27.360 --> 05:29.490 OK so let's see how this works. 05:29.760 --> 05:32.030 Let me say this. 05:32.290 --> 05:34.720 It's what the compiler to verify everything. 05:35.520 --> 05:39.270 OK so let's execute this. 05:39.960 --> 05:41.760 Let me say it again. 05:43.630 --> 05:44.310 OK. 05:44.540 --> 05:45.350 I made a mistake. 05:46.780 --> 05:47.820 OK now it's fine. 05:49.320 --> 05:51.660 Let's execute this now. 05:51.840 --> 05:57.500 I have a more precise error which says I'm not able to handle the designer role. 05:57.660 --> 06:00.200 Now what happens if I don't specify the role here. 06:02.770 --> 06:04.110 We're going to happen is that. 06:04.270 --> 06:07.110 I'm not gonna be able to handle the empty role. 06:07.360 --> 06:10.400 So why don't we create a special rule for that. 06:10.600 --> 06:17.840 We can say if you draw it's equals to empty string which is not specified. 06:18.610 --> 06:27.480 I can return a different error I can't handle empty roles right now. 06:27.500 --> 06:33.150 Also notice that I'm returning 0 as a salary but I could return anything. 06:33.170 --> 06:38.060 It's gonna depend on the color of this function to do something with the value or not. 06:38.720 --> 06:39.650 So 06:43.390 --> 06:50.700 I made a mistake again because it's not asking you and now it says I can handle integrals. 06:51.240 --> 07:00.690 So now let's verify that everything is working fine by assigning a value row there's gonna be Row developer 07:02.460 --> 07:04.850 and now salaries 100. 07:05.040 --> 07:05.390 OK. 07:05.820 --> 07:06.450 So now what. 07:06.460 --> 07:09.810 Check how to meet errors and how to handle them. 07:09.850 --> 07:16.650 And I would like to talk a little bit about some common idioms or practice that people usually do when 07:17.070 --> 07:18.880 handling errors. 07:18.930 --> 07:25.160 The first thing is that as you know functions can return zero or more arguments. 07:25.390 --> 07:32.650 And when you're returning error you usually return one error not more than one. 07:32.710 --> 07:39.550 Even if I could return more than one error in a function we usually do one and the error soul is in 07:39.550 --> 07:40.900 the last place. 07:40.920 --> 07:42.340 So it's the last argument. 07:42.820 --> 07:46.560 Even if I could do error and end. 07:46.900 --> 07:49.240 I mean there's nothing that stops you from doing that. 07:49.840 --> 07:55.430 But the idea is to be idiomatic with the language and do it. 07:55.930 --> 07:57.830 You want to return errors in the end. 07:58.570 --> 08:02.650 Another thing is to check errors in line and you do it like this. 08:02.650 --> 08:09.010 So what you see here is that assigning the return of the salary to variable salary an error and then 08:09.010 --> 08:09.780 I'm taking them. 08:09.960 --> 08:13.120 But I can do this in a single line by doing this. 08:13.210 --> 08:22.510 So if the same line cycle on error different from Neil altering the error. 08:23.360 --> 08:28.320 Otherwise I'd print a salary and this is a very common pattern. 08:28.370 --> 08:30.230 The people usually do. 08:30.230 --> 08:37.910 So when you start reading go code you will see many times a function evaluation and the error revelation 08:37.910 --> 08:40.400 then the effect is exactly the same. 08:40.730 --> 08:50.680 So I have here a hundred and if I make an invalid role it's gonna be the same effect. 08:50.690 --> 08:54.490 So this is lot the last video for this section. 08:54.490 --> 08:57.100 Hopefully you've learned the basic goal. 08:57.210 --> 09:05.390 We've gone through the very basic language construct that we went through the control structure passing 09:05.600 --> 09:10.940 through more complex times like mob slices and your custom complex structures. 09:11.810 --> 09:15.830 And today we'll check how to handle errors. 09:15.830 --> 09:21.830 I hope you enjoy it and that you are ready to the next section which is algorithms.