Table of Contents

Custom errors

What is it?

A custom error is your own error type (usually a struct) that implements `Error() string`. It may include extra fields (code, operation, resource ID, etc.).

What is it used for?

Example

package main
 
import "fmt"
 
type NotFoundError struct {
    Resource string
    ID       string
}
 
func (e *NotFoundError) Error() string {
    return fmt.Sprintf("%s not found: %s", e.Resource, e.ID)
}

Notes / best practice

Hard words (English)