I have a json response from a api as map[message:Login Success. userid:1]
Server:
c.JSON(200, gin.H{"message": "Login Success.", "userid": 1})
Client:
var result map[string]interface{}
json.NewDecoder(resp.Body).Decode(&result)
msg, ok := result["message"].(string)
if !ok {
msg = "Something went wrong."
}
userID, ok := result["userid"].(int)
if !ok {
userID = 0
}
But userID, ok := result["userid"].(int)
always fails. I've even tried using:
switch v := x.(type) {
case nil:
fmt.Println("x is nil")
case int:
fmt.Println("x is", v)
case bool, string:
fmt.Println("x is bool or string")
default:
fmt.Println("type unknown")
}
And it just gave me unknown. Why is the integer not being taken as integer?
It looks like its treating the value as float64
.