In the last lecture, I said we'd make a routine that handles errors, so let's do that.
What I'm going to do is come down here to my main function and create a new path and a new euro that
I'm going to handle.
And I'll call this divide.
And up here.
Of course, we need to point that to the correct function divide, which doesn't exist yet, but that's
no problem.
We can create one func divide.
And because this is a handler, it has two parameters.
The response writer W is KTP Dot response writer and a pointer to a request.
And that's supposed to be a pointer.
So let's make it a pointer and give her curly braces.
And now here I want to call a function that does division.
And the reason I'm doing division is that dividing by zero is generally a bad idea.
So let's create another function.
Func divide values that takes two parameters, X and Y, but this time I'm going to make them float's.
I'll make it a float thirty two, which is another of the basic data types.
So far we have seen strings, we have seen INTs and float.
Thirty two is just a floating point number.
I could have used float 64 if I wanted, but it doesn't matter because I'm going to just use this for
a quick test.
That function is going to return to things.
It's going to return a float thirty two and it's going to return an error.
And if there is no error, it should return nil.
So how do we do this?
First of all, well, the basic logic is going to be the result is assigned X divided by what?
And I'll return Y and nil and that actually works just fine.
I not return.
Why return result and nil.
And that works just fine.
But there's no actual error generated here.
So what I need to check is to make sure that Y is greater than or equal to zero.
So all I have to do is say if Y is less than or equal to zero then I want to stop, I want to generate
an error.
And I can do that by saying error, which is a new variable, is assigned the value of and this is a
built in function errors new and some strength cannot divide by zero.
And at that point I want to return and I'll just say float thirty two I point zero and er so that's
that.
If this, if this function gets the wrong thing here, errors don't new.
Let's try that again.
Errors not new.
There we are.
It just had to import it up here.
Errors return return to zero and the error.
I don't care about the zero.
It's the error that I'll be checking for up here and divide.
I'm not doing anything yet.
So let's actually call this function and I'm going to get two values from it.
I'll get my float, which I'll call F and I'll get an error that will be nil if there is no error but
will be not nil if it is an error.
And that is I get those two values by calling the function I just created divide values and I'll put
in one hundred and one hundred point zero because it's the float and ten point zero.
If error is not equal to nil, then what do I do.
I want to write to the screen so I can just use my standard.
Format or format F, print F.
You cannot divide by zero, and at that point, you think I could just stop because, you know, well,
we've got the air, we've written the scream, let's leave it like that and show you why you can't do
that if there is no air that I actually want to write the result to the screen.
And I can do that by format F F, format s, print F.
And what I'm going to put in is a string and it will be a float.
So the placeholder for float is percent F divided by percent F is percent F and then I have to of course
fill in my placeholders and those would be X, Y and F.
All right.
So what is wrong with this?
We have got to put my response right here in front of it.
Not enough for the wrong place for my place.
Let's try that again.
I think I need more caffeine once again.
That's what I want.
Now, if I run this and I'm going to right now, everything compiles undefined X and undefined Y, let's
find X and Y.
Oh, yes.
I didn't actually have values for X and Y, so I'll just hard to lose.
One hundred and ten.
I'll try that again.
Everything compiles.
I go back to my web browser, I reload the page for the home page that works great.
I go to the about page that does what I want and then I go to the divide page and one hundred divided
by ten is ten and that's correct.
OK, let's go back here and let's try to divide by zero.
So I'll put in zero point zero here and up here as well, or I'm actually doing the math and that should
throw an error.
Right.
Let's see if it does restart the application.
Go back here and reload the page.
And here's why we can't do what I did just a minute ago.
Cannot divide by zero immediately.
Followed by one hundred.
Divided by zero is zero.
Well, that's not a true answer.
Let's find out why I did that here.
I have on line thirty in my application format F Pranav then the response writer then cannot divide
by zero at that point.
I want to return.
I want to stop executing this function.
So let's try it again.
Reload cannot divide by zero, and that's what I'm looking for.
All right, that's why we checked for errors already in the next election, we'll pick up with something
a little more sophisticated.