![]() |
VOOZH | about |
Go handles errors using explicit error values instead of exceptions. The error type is an interface with a single method:
type error interface {
Error() string
}
Any type implementing Error() can be used as an error, allowing developers to create custom errors with meaningful context. This guide covers creating custom errors, implementing them, and advanced techniques for wrapping and unwrapping errors effectively.
Go's built-in error handling is based on returning an error type from functions. If an error is encountered, it is returned to the caller, who can decide what to do with it.
In this example, errors.New creates a simple error with a message. This is helpful for cases where you need a basic error, but it lacks context. For more complex scenarios, custom errors become essential.
Custom errors in Go are essential when:
Custom errors give you the flexibility to add specific fields or methods that describe the error more precisely.
To create a custom error in Go, you need to define a new type that implements the Error() method. This method returns the error message as a string.
Example:
In this example, we’ve defined a custom error type MyError that includes both a Code and a Message. The Error() method formats these values into a string, making it easy to print or log the error.
errors.Is() MethodA sentinel error is a predefined error value that you can compare to errors returned by functions. Go’s errors.Is() method is useful when you need to check if an error is of a specific type or matches a known error.
Example:
In this example, we define a sentinel error ErrNotFound. We then use errors.Is() to check if the error returned by doSomething() matches this sentinel value.
Go 1.13 introduced the %w verb in fmt.Errorf() for wrapping errors, enabling the preservation of the original error while adding more context.
Example:
In this example, fmt.Errorf() is used to wrap the ErrNotFound error with more context ("failed to do something"). The %w verb ensures the original error is preserved, and you can still use errors.Is() to check for the root cause.
Unwrap()Go’s error system also allows you to unwrap errors. By implementing the Unwrap() method in your custom error types, you allow deeper inspection of the underlying cause of an error.
Example:
In this example, MyError implements the Unwrap() method, which returns the original error (ErrNotFound). This allows you to unwrap the error and inspect it for more context.
errors.As()The errors.As() function is useful when you want to extract the custom error type and access its specific fields.
Example:
In this example, errors.As() is used to check if the error is of type MyError and extract its value.
Unwrap() to gain deeper insights into the root cause.Custom errors in Go provide developers with a flexible and powerful tool to manage errors efficiently. By creating your own error types, wrapping errors with context, and unwrapping them when needed, you can build a robust error-handling mechanism that improves the overall quality of your Go applications. Whether you need more context for debugging, categorizing errors, or improving the clarity of your code, custom errors are an essential feature in Go.