Essential Go Error handling  Edit on GitHub      File Issue

Create custom error type

In Go any type that implements built-in error interface (i.e. Error() string method) can be used as error:

// MyError is a custom error type
type MyError struct {
	msg string
}

func (e *MyError) Error() string {
	return e.msg
}

func printError(err error) {
	fmt.Printf("%s\n", err)
}

func main() {
	printError(&MyError{msg: "custom error type"})
}
custom error type
  ↑ ↓ to navigate     ↵ to select     Esc to close