panic interface conversion interface {} 是 float64 不是 int64
panic interface conversion interface {} is float64 not int64
我收到以下错误
panic: interface conversion: interface {} is float64, not int64
我不确定 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
}
错误似乎来自行 ad.Refresh = payload["refresh"].(int64)
我想我只需要知道如何将接口类型从 float64 转换为 int64,反之亦然 {}
我已尽一切努力将类型改回 int64,但出现一个或另一个错误,现在需要帮助才能继续前进
这里是 cookie 中的负载数据经过 jwt 解码后的样子的示例
{
"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))
你需要先assert the accurate dynamic type of the interface value and then, if successful, you can convert它到你想要的类型。
请注意,接口值为 float64
的原因是因为这是 encoding/json
解码器在将 JSON 数字解组为 interface{}
值时的默认设置。
To unmarshal JSON into an interface value, Unmarshal stores one of
these in the interface value:
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
我收到以下错误
panic: interface conversion: interface {} is float64, not int64
我不确定 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
}
错误似乎来自行 ad.Refresh = payload["refresh"].(int64)
我想我只需要知道如何将接口类型从 float64 转换为 int64,反之亦然 {}
我已尽一切努力将类型改回 int64,但出现一个或另一个错误,现在需要帮助才能继续前进
这里是 cookie 中的负载数据经过 jwt 解码后的样子的示例
{
"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))
你需要先assert the accurate dynamic type of the interface value and then, if successful, you can convert它到你想要的类型。
请注意,接口值为 float64
的原因是因为这是 encoding/json
解码器在将 JSON 数字解组为 interface{}
值时的默认设置。
To unmarshal JSON into an interface value, Unmarshal stores one of these in the interface value:
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