当我的Error struct 包含嵌套在其中的片时,Errs.is似乎不能正常工作:
package main
import (
"errors"
"fmt"
"os"
)
type Response struct {
Details []string
}
type ErrResponseError struct {
Response Response
}
func (err ErrResponseError) Error() string {
return "response error"
}
func main() {
err := ErrResponseError{}
fmt.Fprintf(os.Stdout, "equal: %v", errors.Is(err, ErrResponseError{}))
}
返回
等于:FALSE
package main
import (
"errors"
"fmt"
"os"
)
type Response struct {
Details string // Changed this line
}
type ErrResponseError struct {
Response Response
}
func (err ErrResponseError) Error() string {
return "response error"
}
func main() {
err := ErrResponseError{}
fmt.Fprintf(os.Stdout, "equal: %v", errors.Is(err, ErrResponseError{}))
}
返回
等于:真
................................................................................................................................................................................................................................................