|
Hi ! ====== but I don't know how to get the data from this ? it works but i got : changed to the |
Answered by
aldas
Oct 24, 2022
Replies: 1 comment 1 reply
|
You need to cast into correct type value, ok := c.Get("KEY").(time.Time)
if !ok {
return errors.New("invalid type for KEY")
}NB: if you set it as pointer you need to cast it back to pointer. Or if you set value into context and its type is pointer to or longer example: func main() {
e := echo.New()
e.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
c.Set("KEY", time.Now())
return next(c)
}
})
e.GET("/", func(c echo.Context) error {
value, ok := c.Get("KEY").(time.Time)
if !ok {
return errors.New("invalid type for KEY")
}
return c.JSON(http.StatusOK, map[string]time.Time{"time": value})
})
if err := e.Start(":8080"); err != http.ErrServerClosed {
log.Fatal(err)
}
} |
1 reply
Answer selected by
topben
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You need to cast into correct type
NB: if you set it as pointer you need to cast it back to pointer. Or if you set value into context and its type is pointer to
token.Payloadyou need to cast it back to*token.Payloadwhen you Get it from token.api.Payloadis different type/struct I assume.or longer example: