diff --git a/pg/pg.go b/pg/pg.go index 65967d8..035a531 100644 --- a/pg/pg.go +++ b/pg/pg.go @@ -25,14 +25,13 @@ type PgxQuerier interface { // Usage: // // type User struct { -// id int -// name string +// id int `db:"id"` +// name string `db:"name"` // } // // db := pgx.Connect(context.Background(), "") -// users, err := pgUtils.Select[User](context.Background(), db, "SELECT * FROM users") +// users, err := pgUtils.Select[User](context.Background(), db, "SELECT id, name FROM users") func Select[T any](ctx context.Context, db PgxQuerier, query string, args ...any) (out []*T, err error) { - out = []*T{} rows, err := db.Query(ctx, query, args) if err != nil { switch { @@ -47,7 +46,8 @@ func Select[T any](ctx context.Context, db PgxQuerier, query string, args ...any for i, fd := range rows.FieldDescriptions() { columns[i] = fd.Name } - itemFieldPtrs := make([]interface{}, len(columns)) + itemFieldPtrs := make([]any, len(columns)) + out = []*T{} defer rows.Close() for rows.Next() {