如何检查恢复中错误的确切类型?

I'm following this example here:

https://www.socketloop.com/tutorials/golang-smarter-error-handling-with-strings-contains-function

 if !strings.Contains(err.Error(), "timed out") {
    fmt.Printf("resulting error not a timeout: %s", err)
 }

However, if I do that in my code I will get this

err.Error undefined (type interface {} is interface with no methods)

I wonder what I'm doing wrong here, and how should I check for an exact error (in my case I want to recover from the error "slice bounds out of range")

Best,

Use type assertion to get error https://play.golang.org/p/BryV7YfZHS

defer func(){
    err := recover().(error)
    fmt.Println(err.Error())
}()

a := make([]int, 0)
a[1] = 0

But I suggest you don't do that. First, you should not use recover. In almost all cases that means you're doing something wrong. Instead, fix the code that panics.

Second, don't use string matching to determine the cause of the error. There're many reasons why it's bad but I will just say that it's not the Go way of doing things. I understand that you may be using API that just doesn't leave you a choice but try to find a better way. Use type assertion, type switch to get the exact error type and go from there.