#Trying to query a database with a gin GET request

10 messages · Page 1 of 1 (latest)

wise goblet
#
type Users struct {
    id        int
    user_name string
}```
You are not exporting any of the fields.
#

I suggest go type Users struct { Id int `json:"id"` UserName string `json:"user_name"` }

#

I also highly recommend querying the actual rows you want, rather than saying SELECT *

#

Possibly SELECT id, user_name FROM etc etc

#

Yes, it's telling encode/json what to call the fields when it serializes.

#

Not to the problem you currently have. That's because you're not exporting the fields.
It will, however, make the whole thing a lot tidier and slightly more performant.

#

Another minor thing like that, is that the struct called Users, plural, contains data for a singular user.
Perhaps it should be called User?

#

No, Gin will handle that bit for you. What you need to worry about is that Go does not export the fields unless their name starts with an upper case letter.

#

That, of course, means you'll have to rewrite err := rows.Scan(&u.id, &u.user_name) as well.