Senior
Which standard library function should be used to extract a specific type of error?
sobes.tech AI
Answer from AI
In Go, to extract a specific type of error from an error interface, the errors.As function from the standard errors package is used.
It allows you to check whether the error or any of the nested errors match a specified type, and access that error.
Example usage:
var targetErr *MyErrorType
if errors.As(err, &targetErr) {
// err or one of the nested errors has type *MyErrorType
// You can work with targetErr
}
Thus, errors.As is the standard and recommended way to safely extract a specific type of error.