In Go, error handling is usually done using the built-in error
type, which is an interface that represents an error with a single error message. However, sometimes you may need more structured error types to provide additional information or context.
Here are the steps to create structured error types in Go for better error handling:
type MyError struct {
message string
code int
}
error
interface: To make your custom error type compatible with the built-in error handling mechanism, you need to implement the Error()
method of the error interface.func (e MyError) Error() string {
return fmt.Sprintf("MyError: %s (code: %d)", e.message, e.code)
}
func SomeFunction() error {
if err := SomeOtherFunction(); err != nil {
return MyError{
message: "Something went wrong",
code: 500,
}
}
return nil
}
err := SomeFunction()
if err != nil {
if myErr, ok := err.(MyError); ok {
// Handle the specific error type
fmt.Println("Error message:", myErr.message)
fmt.Println("Error code:", myErr.code)
} else {
// Handle other error types
fmt.Println("Unknown error:", err)
}
}
By creating structured error types, you can provide more context and information about the errors, making it easier to handle and debug them. Additionally, type assertions allow you to handle different error types differently, providing more granular error handling logic.