errors.As recursively unwraps an error chain and reports whether any error matches a specified type. It is needed when %w wrapping hides the original type from a direct type switch. section 7.3 section 7.8
A wrapped transient error bypasses a type switch
After getTransactionAmount wraps a transientError with fmt.Errorf("failed to get transaction %s: %w", transactionID, err), switch err := err.(type) no longer matches transientError. Use errors.As instead: if errors.As(err, &transientError{}) { http.Error(w, err.Error(), http.StatusServiceUnavailable) } else { http.Error(w, err.Error(), http.StatusBadRequest) }. The target argument must be a pointer; otherwise the code compiles but panics at runtime. section 7.3
Use errors.As when callers must classify a specific error type regardless of whether it is returned directly or wrapped with additional context. section 7.3



