jeudi 14 juillet 2016

Other ways of verifying reflect.Type for int and float64

In golang, a number in JSON message is always parsed into float64. In order to detect if it is actually integer, I am using reflect.TypeOf() to check its type. Unfortunately there is no constant that represents reflect.Type.

intType := reflect.TypeOf(0)
floatType := reflect.TypeOf(0.0)
myType := reflect.ValueOf(myVar)
if myType == intType {
    // do something
}

Is there more elegant solution instead of using 0 or 0.0 to get reflect.Type?





Aucun commentaire:

Enregistrer un commentaire