func New(text string) error
Error handling is essential in the development process, and using functions can create an object that represents a specific error. Accepts a parameter of string type (used to describe error information) and returns a value of type error. For example:
package main import "errors" func main() { err := ("invalid input") }
The error type is an interface defined in the builtin package, defined as follows:
type error interface { Error() string }
It can be seen that this interface defines a basic Error method to return the description information of the error.
package main import ( "errors" "fmt" ) func main() { err := ("invalid input") errDesc := () (errDesc) // Output invalid input}
func Is(err, target error) bool
Used to determine whether a given error is a target error type or an error wrapped based on the target error type, the error chain will be checked recursively until the target error type is found or the end of the error chain is reached. Return true if the target error type is found, otherwise return false. See an example:
package main import ( "errors" "fmt" ) func main() { err := ("invalid input") err1 := ("invalid input") err2 := ("err2: [%w]", err) ((err1, err)) // false ((err2, err)) // true }
Because err and err1 are both created using the function, using Is will return false. err2 is wrapped based on err, so using Is will return true.
func As(err error, target any) bool
For converting an error to a specific type of error, the As function checks whether err is an instance of the type pointed to by target, and if so, assigns the instance to target and returns true. Otherwise, return false. See an example:
package main import ( "errors" "fmt" ) type MyError struct { Message string } func (e *MyError) Error() string { return } func main() { err := &MyError{ Message: "This is a custom error", } var target *MyError if (err, &target) { ("Custom error found:", ) } else { ("Custom error not found") } }
Use err to check whether err is an instance of type MyError and assign the instance to target. Run the example to see the effect
$ go run Custom error found: This is a custom error
This function is usually used to deal with different types of errors and adopt corresponding processing methods according to the type of error.
func Unwrap(err error) error
Used to expand an error object to get the next layer of error object. If the error object does not have the next layer of error object, it returns nil. See an example:
package main import ( "errors" "fmt" ) func main() { originalErr := ("original error") err := ("error: %w", originalErr) unwrappedErr := (err) (unwrappedErr) // Output: original error}
Use the function to wrap originalErr and get err, and then unwrap err by using Unwrap and get the error object before packaging, originalErr. It should be noted that Unwrap can only expand the wrapped error object. If you want to expand other types of error objects, you can use the type assertion operator `.` for type assertion.
summary
The errors package provides some simple and easy-to-use functions to handle and obtain error information. With the errors package, you can achieve very powerful error handling functions.
This is the end of this article about a detailed explanation of the errors package in Golang. For more information about the Golang errors package, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!