检查值是否为整数

So Im sending this payload to my app :

{
    "name" : "Matias Barrios",
    "age" : 123
}

The problem I am facing is that when I test if name is a string it works perfectly. But test if age is an int is always returning false, no matter what I do.

if gjson.Get(spec, "name").Exists() {
            if _, ok := gjson.Get(spec, "name").Value().(string); !ok {
                n := validator_error{Path: "_.name", Message: "should be a string"}
                errors = append(errors,n)
            }
}

if gjson.Get(spec, "age").Exists() {
            if _, ok := gjson.Get(spec, "age").Value().(int); !ok {
                n := validator_error{Path: "_.age", Message: "should be an int"}
                errors = append(errors,n)
            }

        }

Could someone tell me where is the error here?

Note - I am using this https://github.com/tidwall/gjson to get the values out of the JSON.

It seems like this lib for json numbers it return float64

bool, for JSON booleans
float64, for JSON numbers
string, for JSON string literals
nil, for JSON null

As in github.com/tidwall/gjson result type can only hold float64 for json numbers.

Though you can use Int() instead of Value() to get an integer value.

fmt.Println(gjson.Get(spec, "age").Int())  // 123

You can use the result.Type field to check the type:

res := gjson.Get(spec, "age")
if res.Type != gjson.Number {
    n := validator_error{Path: "_.age", Message: "should be an int"}
}

If you require that the value is a whole number 123 not 123.5:

res := gjson.Get(spec, "age")
if res.Type != gjson.Number || math.Floor(res.Num) != res.Num {
    n := validator_error{Path: "_.age", Message: "should be an int"}
}