Error verification
In Chapter 4, Building Efficient Test Suites, we briefly discussed Go’s approach of explicit error handling. We learned that errors are typically returned last in a list of multiple return values. So far, we have been using Go’s inbuilt error
type and representative error messages to indicate to the user when something has gone wrong. Let us now take a closer look at how error verification works in Go.
We have created errors in two ways so far. The simplest way is using the errors.New
function. It creates an error with a given message:
err := errors.New("Something is wrong!")
This function takes in an error message as a parameter and returns the error
interface type. In order to get our error message back, we invoke the Error
method on the function:
msg := err.Error()
This method returns the message as a string type.
Writing a test to compare the incoming and outgoing error messages is trivial for this example:
func TestErrorsVerification...