page

package
v0.50.0 Latest Latest
Warning

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

Go to latest
Published: Aug 31, 2026 License: Apache-2.0 Imports: 0 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DefaultPageNumber is the default page number for pagination (starts from 1).
	DefaultPageNumber int = 1
	// DefaultPageSize is the default page size for pagination.
	DefaultPageSize int = 15
	// MaxPageSize is the maximum allowed page size to prevent excessive data loading.
	MaxPageSize int = 1000
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Page

type Page[T any] struct {
	Page  int   `json:"page"`
	Size  int   `json:"size"`
	Total int64 `json:"total"`
	Items []T   `json:"items"`
}

Page represents a paginated response with metadata and items.

func New

func New[T any](pageable Pageable, total int64, items []T) Page[T]

New creates a new page from pageable parameters, total count, and items. It ensures items is never nil and returns an empty slice if needed.

func (Page[T]) HasNext

func (page Page[T]) HasNext() bool

HasNext returns true if there are more pages after the current one.

func (Page[T]) HasPrevious

func (page Page[T]) HasPrevious() bool

HasPrevious returns true if there are pages before the current one.

func (Page[T]) Map added in v0.50.0

func (page Page[T]) Map[R any](convert func(T) R) Page[R]

Map converts a page's items to another type, carrying the pagination metadata across unchanged.

Turning a Page[Model] into a Page[VO] is the last step of nearly every list endpoint, and hand-rolling it means restating Page, Size and Total at each call site — the three fields a conversion has no business touching. The method form is what keeps that from happening: before Go 1.27 a method could not introduce a type parameter, so this had to be a free function taking the page as an argument, which reads no better than the loop it replaces.

Like New it never produces a nil slice, so an empty page still serializes as [] rather than null.

func (Page[T]) TotalPages

func (page Page[T]) TotalPages() int

TotalPages returns the total number of pages based on the total count.

type Pageable

type Pageable struct {
	Page int `json:"page"` // 1-based
	Size int `json:"size"`
}

Pageable represents pagination parameters for querying data.

func (*Pageable) Normalize

func (p *Pageable) Normalize(size ...int)

Normalize normalizes the pageable parameters. The optional size argument supplies the fallback page size used when Size is unset; only the first value is considered and any further arguments are ignored. When omitted, DefaultPageSize is used.

func (Pageable) Offset

func (p Pageable) Offset() int

Offset returns the zero-based offset for database queries.

Jump to

Keyboard shortcuts

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