type MyError struct {
errors []string
}
func (t *MyError) Error() string {
if t == nil {
fmt.Println("t ptr empty")
return ""
}
pointers := make([]string, 0, len(t.errors))
for i, r := range t.errors {
pointers[i] = r
}
sort.Strings(pointers)
return fmt.Sprintf("n error(s) decoding:
%s", len(t.errors), strings.Join(pointers, ","))
}
func main() {
var err *MyError
err.Error() // expected "panic: runtime error: invalid memory address or nil pointer dereference" here
}
The variable err is nil so calling err.Error() method is expected to cause a panic "runtime error: invalid memory address or nil pointer dereference", but the method call succeeds. Why doesn't this panic?