How to Implement the error Interface in Go

Implement the error interface in Go by defining a type with an Error() string method.

Implement the error interface by defining a type with a method named Error that returns a string. Any type with this method automatically satisfies the interface.

type MyError struct {
	msg string
}

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

func main() {
	var err error = MyError{"something went wrong"}
	fmt.Println(err) // prints: something went wrong
}