In this venue we will learn how to deal with failures.
This is handling in a meeting at our values.
Finally we will learn some common idioms and treat errors.
Things failed.
They always do.
And software is no exception.
To deal with this situation go provides an error type specifically designed to handle these cases to
create an error.
We can use the error package.
So let's take a look at the documentation.
This negative contagion of the package.
And as you can see it only exposes one function.
I think this the probably most simple package of the standard library.
So the only function that expose it is new which receives an arguments text of string type and it returns
an error.
So this broadly the theme that we're all gonna be using for these examples.
Remember if you want to change segmentation go to Golden dot org slash EKG slash error to review it
or to get more information about it.
So to start I'd like to go back to the example we saw in the previous video which were about math of
instructors and I would like to expand this by handling this case correctly.
The case where the role is not developer or not architect.
So right now what we're doing is we're just returning zero but it really makes sense to return an error
saying a the road you specified is not percent or I don't know how to handle this.
So let's change these by instead of returning just an integer.
Remember that go functions can return more than one volume so let's return error here.
Right.
So if we do this this should fail because we're just returning 1 value here which is a hundred but it
should be an integer and an error.
But there is no error here so let's just return nil which means nothing.
So as you can see this line is fine now then let's return Neil here because it's also valid.
There's no error here but here in this thing the error case.
So let's return to zero but let's also return an error.
So how do we do this.
We first need to import the error package which is here we are ready so how do we import packages.
And now let's create an error and let's say go or let's say not a will handle this role.
OK.
So now I'm returning an error.
Let's say this.
And the compiler says that everything is fine.
So let's create an ether.
We already saw how to it and let's say that name is Olga and the role will be designer.
Right.
So the salary function doesn't know how to handle the role the designer.
So let's try to execute using the salary and now it's returning to these values.
Let's get the salary here and the error here.
We usually use the error word E R to handle errors variables.
So the type is error and usually this is an idiom.
This is not like it doesn't has to be like this that it's a common pattern go program that you will
see error 2 refers to error variable and now let's see if error is not Neil which means either there
was an error ban equals means different from so let's pretend otherwise as we did before we can print
salary salary.
OK now let's execute this OK.
I'm not humbled to I'm not able to handle this role right now.
If you take a look here the argument of error is new.
It's a string.
So what happened is I would like to be more precise about this error and bring the role that you're
sending to me in the message.
Right.
So what I can do is to use a nested function.
I'm gonna use f empty aspirin F this is very interesting how it works and I'm gonna say the same I'm
not able to handle the person that's s role and what does this mean.
This is going to interpolate variables so I have the role here and I'm saying in these simple person
touch est embeds the variable that I'm passing you are the first argument.
OK so let's see how this works.
Let me say this.
It's what the compiler to verify everything.
OK so let's execute this.
Let me say it again.
OK.
I made a mistake.
OK now it's fine.
Let's execute this now.
I have a more precise error which says I'm not able to handle the designer role.
Now what happens if I don't specify the role here.
We're going to happen is that.
I'm not gonna be able to handle the empty role.
So why don't we create a special rule for that.
We can say if you draw it's equals to empty string which is not specified.
I can return a different error I can't handle empty roles right now.
Also notice that I'm returning 0 as a salary but I could return anything.
It's gonna depend on the color of this function to do something with the value or not.
So
I made a mistake again because it's not asking you and now it says I can handle integrals.
So now let's verify that everything is working fine by assigning a value row there's gonna be Row developer
and now salaries 100.
OK.
So now what.
Check how to meet errors and how to handle them.
And I would like to talk a little bit about some common idioms or practice that people usually do when
handling errors.
The first thing is that as you know functions can return zero or more arguments.
And when you're returning error you usually return one error not more than one.
Even if I could return more than one error in a function we usually do one and the error soul is in
the last place.
So it's the last argument.
Even if I could do error and end.
I mean there's nothing that stops you from doing that.
But the idea is to be idiomatic with the language and do it.
You want to return errors in the end.
Another thing is to check errors in line and you do it like this.
So what you see here is that assigning the return of the salary to variable salary an error and then
I'm taking them.
But I can do this in a single line by doing this.
So if the same line cycle on error different from Neil altering the error.
Otherwise I'd print a salary and this is a very common pattern.
The people usually do.
So when you start reading go code you will see many times a function evaluation and the error revelation
then the effect is exactly the same.
So I have here a hundred and if I make an invalid role it's gonna be the same effect.
So this is lot the last video for this section.
Hopefully you've learned the basic goal.
We've gone through the very basic language construct that we went through the control structure passing
through more complex times like mob slices and your custom complex structures.
And today we'll check how to handle errors.
I hope you enjoy it and that you are ready to the next section which is algorithms.