i'm a new gopher and got really confused by variable types, if i'm defining a custom error
import (
"fmt"
"reflect"
)
// custom errors
type myError struct {
msg string
}
func (m *myError) Error() string {
return m.msg
}
func errorGen() error {
return &myError{"custom error"}
}
generate a new error and check the type of it
func main() {
e := errorGen()
fmt.Println(reflect.TypeOf(e).Kind()) // type = pointer
// first type assertion
_, ok := e.(error)
if ok {
fmt.Println("type assertion error") // type = error
}
// second type assertion
_, ok = e.(*myError)
if ok {
fmt.Println("type assertion *myError") // type = pointer
}
}
so in above code variable 'e' shows 2 types at the same time. What exactly is e's type? and why "error" is an interface and also can be used as return type?
thank you very much