Table of Contents

error interface

What is it?

`error` is a built-in interface type in Go:

type error interface {
    Error() string
}

Any type that implements `Error() string` can be used as an error.

What is it used for?

Example

type MyError struct{ Msg string }
 
func (e MyError) Error() string { return e.Msg }
 
func f() error {
    return MyError{Msg: "something went wrong"}
}

Notes

Hard words (English)