Golang cast to a custom type fails if I wrap my object

前端 未结 1 1884
南旧
南旧 2021-01-21 12:58

In my app I use validator.v9 to validate my model. After validation I can cast the error interface and it\'s successes, I see \'OK\' on the console

1条回答
  •  清酒与你
    2021-01-21 13:13

    Your errValidation type and the validator.ValidationErrors type are completely different, distinct types. If an interface value holds a value of concrete type errValidation, you can't type assert another concrete type from it, only errValidation.

    So this will work:

    e := ValidationError(errors.New("some err"))
    if _, ok := e.(errValidation); ok {
        fmt.Println("ValidationError: OK")
    } else {
        fmt.Println("ValidationError: FALSE")
    }
    

    And output will be (try it on the Go Playground):

    ValidationError: OK
    

    0 讨论(0)
提交回复
热议问题