I am writing some authentication using Golang, and I have the following code that populates the UserResponse if UserID is not nil, but when it IS nil, the sent UserResponse looks like this:
{
"user": {
"id": 0,
"email": ""
}
}
My code is
func (app *Application) init(w http.ResponseWriter, r *http.Request) {
userID := r.Context().Value("userID")
type UserResponse struct {
ID int `json:"id"`
Email string `json:"email"`
}
var user UserResponse
if userID != nil {
query := "select id, email from users where id = ?"
row := app.Db.QueryRow(query, userID)
err := row.Scan(&user.ID, &user.Email)
if err != nil {
fmt.Println(err.Error())
}
fmt.Println(user)
}
app.writeJSON(w, http.StatusOK, user, "user")
}