课程: Go Practice: Error Handling
今天就学习课程吧!
今天就开通帐号,24,700 门业界名师课程任您挑!
Solution: Panic and recover
- [Instructor] And I'm going to show you an example of how you would solve this in Go. So the first thing that you're going to do is we're going to add a defer function that will prevent everything being called at the same time in order for us to call the recover method. So within this defer function, we're going to say, if r is equal to recover and r doesn't equal nil, we'll add a print statement that says recovered, and we'll pass in the error messaging that may come from the recover. R. Once that has been done within the if block, we will also make sure we add our parentheses at the end. And then underneath, we're going to check if whether or not the number is even or odd. So we're going to determine this based off of checking with whether or not the remainder of the number divided by two is zero or not. And in this case, we will be checking whether or not it is not equal to zero. And if it's not, we will call our…