Documentation
¶
Index ¶
Constants ¶
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 ¶
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]) HasPrevious ¶
HasPrevious returns true if there are pages before the current one.
func (Page[T]) Map ¶ added in v0.50.0
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 ¶
TotalPages returns the total number of pages based on the total count.
type Pageable ¶
Pageable represents pagination parameters for querying data.