如何检查错误是否为“超过最后期限”错误?

I'm sending a request with a context which specified with a 10 seconds timeout:

ctx, cancel := context.WithTimeout(context.Background(), time.Second * 10)
defer cancel()
_, err := client.SendRequest(ctx)
if err != nil {
    return 0, err
}

now when I hit that timeout the error message is confusing:

context deadline exceeded

Is it possible to check if the err is the timeout error so that I can print a nicer error message?

ctx, cancel := context.WithTimeout(context.Background(), time.Second * 10)
defer cancel()
_, err := client.SendRequest(ctx)
if err != nil {
    if isTimeoutError(err) {
       return nil, fmt.Errorf("the request is timeout after 10 seconds") 
    }
    return nil, err
}

How to implement such isTimeoutError function?

You can determine if an error is the result of a context timeout by comparing the error to context.DeadlineExceeded:

 if err == context.DeadlineExceeded {
     // context deadline exceeded
 }

You can determine if an error is any timeout error using the following function:

func isTimeoutError(err error) bool {
     e, ok := err.(net.Error)
     return ok && e.Timeout()
}

This function returns true all timeout errors including the value context.DeadlineExceeded. That value satisfies the net.Error interface and has a Timeout method that always returns true.