jeudi 31 mars 2016

Why int32(0) is not reflect.DeepEqual to type Zero in Golang?

I found this sort of validation in go-swagger package.

// Required validates an interface for requiredness
func Required(path, in string, data interface{}) *errors.Validation {
    val := reflect.ValueOf(data)
    if reflect.DeepEqual(reflect.Zero(val.Type()), val) {
        return errors.Required(path, in)
    }
    return nil
}

I tried to use it and it forced me for some thoughts.

Why the following is not a true statement?

exper := int32(0)
reflect.DeepEqual(
   reflect.Zero(reflect.ValueOf(exper).Type()), 
   reflect.ValueOf(exper)
)





Aucun commentaire:

Enregistrer un commentaire