How do I paginate query results using database/sql with Postgres?

Paginating query results in a PostgreSQL database using the Go language and the `database/sql` package can be done effectively by utilizing the `LIMIT` and `OFFSET` SQL clauses. This allows you to control the number of records returned by your SQL queries, making it easier to manage and display large datasets.

Here’s an example of how you can implement pagination in Go:

package main import ( "database/sql" "fmt" "log" "net/http" _ "github.com/lib/pq" ) func main() { db, err := sql.Open("postgres", "user=username dbname=mydb sslmode=disable") if err != nil { log.Fatal(err) } defer db.Close() page := 1 // Current page number pageSize := 10 // Number of records per page offset := (page - 1) * pageSize rows, err := db.Query("SELECT id, name FROM users ORDER BY id LIMIT $1 OFFSET $2", pageSize, offset) if err != nil { log.Fatal(err) } defer rows.Close() for rows.Next() { var id int var name string if err := rows.Scan(&id, &name); err != nil { log.Fatal(err) } fmt.Printf("ID: %d, Name: %s\n", id, name) } if err := rows.Err(); err != nil { log.Fatal(err) } }

Go PostgreSQL Pagination database/sql LIMIT OFFSET SQL