This is my first question regarding GoLang please be gentle. I am building a test JSON API.
I have a type struct named User
type User struct {
UserID int
Email string
FirstName string
LastName string
PasswordHash string
}
In middleware, I verify JWT token is valid and if so, I load entire record of User and save it in the context so that it is available in handlers under.
So in middleware, I save User struct like this
context.Set(r, "User", *User)
fmt.Println(*User)
next.ServeHTTP(w, r)
Output of Println is
{12 sallu@domain.com.au Mr. Sallu $2a$14oUg3d1rm./.B/vUyhYR9/hlHfagE4tGicNc14EWK3u }
Then in the final handler I access this context and get User object.
User := context.Get(r, "User")
fmt.Println("---User---", fmt.Sprintf(" %T ", User))
fmt.Println(User)
Output of Println is
---User--- models.User
{12 sallu@domain.com.au Mr. Sallu $2a$14oUg3d1rm./.B/vUyhYR9/hlHfagE4tGicNc14EWK3u }
The issue is that in Controller when I access User.Email I am getting an error
User.Email undefined (type interface {} is interface with no methods)
How can I fix this..? I want to use User.Email or User.UserID. Seems like context is doing something to it.
Please help