I am creating a custom error and used it as a function return:
type MyError struct {
Name string
}
func (me *MyError) Error() string {
return me.Name + " is my error"
}
Function that returns the custom error:
func (anyStruct *AnyStruct) Validate() error {
// if Any verification {
return &MyError{Name: "MyError"}
//}
}
Why can't I return as a value instead of a reference? If I try to return as a value:
return MyError{Name: "MyError"}
The editor's linter complains about an error:
cannot use (MyError literal) (value of type MyError) as error value in return statement: missing method Error
Also, I am confused by the fact that a reference is being returned but the function signature does not inform that a pointer will be returned:
func (anyStruct *AnyStruct) Validate() error {
instead of func (anyStruct *AnyStruct) Validate() *error {