pagination

package
v1.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 28, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package pagination provides offset-based pagination for HTTP handlers and repositories.

In the handler

page := pagination.OffsetFromQuery(c.Request)  // parses ?page=&limit=
users, total, _ := svc.List(ctx, filter, page)
totalPages := pagination.TotalPages(total, page.Limit())

In the repository

query.Offset(page.Offset()).Limit(page.Limit())

Defaults and limits

  • Default page: 1
  • Default limit: 20
  • Maximum limit: 100 (capped automatically)

Package pagination provides offset-based pagination primitives for HTTP handlers and GORM repositories.

Usage in handler:

page := pagination.OffsetFromQuery(r)
users, total, _ := svc.List(ctx, filter, page)
totalPages := pagination.TotalPages(total, page.Limit())

Usage in repository:

query.Offset(page.Offset()).Limit(page.Limit())

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func TotalPages

func TotalPages(total int64, limit int) int

TotalPages calculates the total number of pages given total record count and page size.

Types

type OffsetPage

type OffsetPage struct {
	// contains filtered or unexported fields
}

OffsetPage holds validated offset-based pagination parameters.

func NewOffsetPage

func NewOffsetPage(page, limit int) OffsetPage

NewOffsetPage creates an OffsetPage with safe bounds: page is clamped to [1, ∞), limit is clamped to [1, 100].

func OffsetFromQuery

func OffsetFromQuery(r *http.Request) OffsetPage

OffsetFromQuery parses ?page= and ?limit= from the HTTP request. Missing or invalid values fall back to safe defaults.

func (OffsetPage) Limit

func (p OffsetPage) Limit() int

Limit returns the SQL LIMIT value.

func (OffsetPage) Offset

func (p OffsetPage) Offset() int

Offset returns the SQL OFFSET value: (page-1) * limit.

func (OffsetPage) PageNumber

func (p OffsetPage) PageNumber() int

PageNumber returns the current page number (1-based).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL