简体   繁体   English

恐慌接口转换接口 {} 是 float64 而不是 int64

[英]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.

相关问题 恐慌:接口转换:接口 {} 是字符串,而不是 float64 - panic: interface conversion: interface {} is string, not float64 接口转换:接口{}是float64而不是[]接口{} PubNub - interface conversion: interface {} is float64 not []interface {} PubNub 接口转换:接口{}是int64,不是[]uint8 - interface conversion: interface {} is int64, not []uint8 从 int64 转换为 float64 是否安全? - It it safe to convert from int64 to float64? 在Golang中将未知接口转换为float64 - Converting unknown interface to float64 in Golang 为什么当int64(0.5 * float64(time.Minute))不在golang 1.9.2中时,int64(1.1 * float64(time.Minute))是错误? - Why int64(1.1*float64(time.Minute)) is an error while int64(0.5*float64(time.Minute)) is not in golang 1.9.2? 在golang中将Json.Number转换为int / int64 / float64 - Convert Json.Number into int/int64/float64 in golang 不同类型反射的Golang JSON数组:float64 vs int64 - Golang JSON array of different types reflection: float64 vs int64 无法将“float64”类型的表达式转换为“int64”类型 - Cannot convert expression of type 'float64' to type 'int64' Golang-在转换为float64的接口上未定义比较运算符 - Golang - Comparison operator not defined on interface converted to float64
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM