If you don't care about the actual contents of the rows use a select-count query.
var count int
row := repo.DBConn.QueryRow("SELECT COUNT(*) FROM public.initiator where init_id in (select init_id from public.events where request_id=$1)",request_id)
if err := row.Scan(&count); err != nil {
return err
}
If you care about the contents then scan the rows into a slice and then get its length using the len
function.
rows, err := repo.DBConn.Query(`SELECT init_id, email, address, phone, name, zipcode , about,backgroundimg_url,icon_url FROM public.initiator where init_id in (select init_id from public.events where request_id=$1)",request_id`)
if err != nil {
return err
}
defer rows.Close()
var inits []*Initiator
for rows.Next() {
ini := new(Initiator)
if err := rows.Scan(&ini.InitID, &ini.Email, ...); err != nil {
return err
}
inits = append(inits, ini)
}
if err := rows.Err(); err != nil {
return err
}
count := len(inits)
If you're doing pagination and all you want is just, say a 20 rows per request, but you also want the total number of rows that satisfies the WHERE clause, then you need to execute both queries, the one to retrieve the 20 rows and the one to count the total number of rows. You can combine the above examples to do that.