Custom error types

suggest change

In Go error type is a built-in interface with a single method Error() string.

All rules governing interfaces apply to error type.

Any type with an Error() string method can be stored in an error variable. Error has a pointer receiver here, so *MyError implements error and MyError does not.

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

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

	var err error = &MyError{msg: "This is custom error type"}
	fmt.Printf("err: %s\n", err)
err: This is custom error type

Why use custom type in addition to built-in ways of creating errors?

Potential use cases are:

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents