[英]panic interface conversion interface {} is float64 not int64
I am getting the following error我收到以下错误
panic: interface conversion: interface {} is float64, not int64
I am not sure where float64 is coming from here I have type set to int64 but not sure where float64 came from我不确定 float64 来自哪里 我将类型设置为 int64 但不确定 float64 来自哪里
type AccessDetails struct {
AccessUuid string `json:"access_uuid"`
Email string `json:"email"`
Refresh int64 `json:"refresh"`
Expiry int64 `json:"expiry"`
Permission string `json:"permission"`
Scope string `json:"scope"`
}
func GetAccessDetails(c *fiber.Ctx) (*AccessDetails, error) {
ad := &AccessDetails{}
cookie := c.Cookies("access_token")
var err error
token, err := jwt.Parse(cookie, func(token *jwt.Token) (interface{}, error) {
return []byte(os.Getenv("ACCESS_SECRET")), nil
})
if err != nil {
return nil, err
}
payload := token.Claims.(jwt.MapClaims)
ad.Email = payload["sub"].(string)
ad.AccessUuid = payload["access_uuid"].(string)
ad.Refresh = payload["refresh"].(int64)
ad.Expiry = payload["exp"].(int64)
ad.Permission = payload["permission"].(string)
ad.Scope = payload["scope"].(string)
return ad, nil
}
The error seems to be from the line ad.Refresh = payload["refresh"].(int64)
I think i just need to know how to convert types from float64 to int64 or vice versa for interfaces {}错误似乎来自行
ad.Refresh = payload["refresh"].(int64)
我想我只需要知道如何将类型从 float64 转换为 int64 或相反的接口 {}
I have tried everything to change the type back to int64 but i get one error or the other and now need help to move forward我已经尝试了一切将类型更改回 int64 的方法,但是我遇到了一个错误,现在需要帮助才能继续前进
here is example of what the payload data looks like from the cookie after it is jwt decoded这是经过 jwt 解码后 cookie 中有效负载数据的示例
{
"access_uuid": "c307ac76-e591-41d0-a638-6dcc2f963704",
"exp": 1642130687,
"permission": "user",
"refresh": 1642734587,
"sub": "test3@example.com"
}
ad.Refresh = int64(payload["refresh"].(float64))
ad.Expiry = int64(payload["exp"].(float64))
You need to first assert the accurate dynamic type of the interface value and then, if successful, you can convert it to your desired type.您需要首先断言接口值的准确动态类型,然后,如果成功,您可以将其转换为您想要的类型。
Note that the reason the interface values are float64
is because that's the default setting of the encoding/json
decoder when unmarshaling JSON numbers into interface{}
values.请注意,接口值为
float64
的原因是因为这是encoding/json
解码器在将 JSON 数字解组为interface{}
值时的默认设置。
To unmarshal JSON into an interface value, Unmarshal stores one of these in the interface value:
要将 JSON 解组为接口值,Unmarshal 将其中之一存储在接口值中:
bool, for JSON booleans float64, for JSON numbers string, for JSON strings []interface{}, for JSON arrays map[string]interface{}, for JSON objects nil for JSON null
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.