Error Handling
Learn about errors in Go.
We'll cover the following...
We'll cover the following...
Errors in Go
Errors are normal values within Go. We can deal with them in the same way we deal with other types, such as int, string, bool, and so on.
How we can define an error
There are three different ways we can define an error:
- By implementing the
errorinterface - By calling the
errors.New()function - By calling the
fmt.Errorf()function
Let’s see the first two methods in action in the code snippet below:
In the example, the program checks for the error and eventually prints it to the console.
defer, panic, and recover
In this snippet below, we see three keywords. The ...