课程: Go Practice: Error Handling

今天就学习课程吧!

今天就开通帐号,24,700 门业界名师课程任您挑!

Solution: Error logging

Solution: Error logging

- [Instructor] And Now I'm going to show you an example of how you would solve this in Go. So the first thing that we're going to do is add the if statement that we'll check to see if the number being passed in is even or odd. So we'll add our if statement, and we'll check if the number being passed in divided by two has a remainder of zero. If it doesn't, that means that the number is odd, but if it does, that means the number is even. Within our if statement, we will create an error object using the errors package, and we'll type in here logged error, even number. Now that we have our error object, we are going to log it. So we'll use the log library, and we'll use the built-in function, Println. And we will just pass in the error object and then we will return the error. And if the number is odd, we don't want to return an error, so we will return, no. And then when we test the code, we'll see that we've logged those…

内容