Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

github.com/gopi-frame/pagination

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/gopi-frame/pagination

  • v0.0.0-20240805064243-950a495be96c
  • Source
  • Go
  • Socket score

Version published
Created
Source

Pagination

Go Reference Go codecov Go Report Card License: MIT

Installation

go get -u -v github.com/gopi-frame/pagination

Import

import "github.com/gopi-frame/pagination"

Usage

Quick Start

package main

import (
	"fmt"
	"github.com/gopi-frame/pagination"
)

func main() {
	numbers := []int{1, 2, 3, 4, 5}
	paginator := pagination.New(numbers, 10, 1, 5)
	fmt.Println(paginator.Items()) // [1, 2, 3, 4, 5]
	fmt.Println(paginator.LastPage()) // 2
	fmt.Println(paginator.Total()) // 10
}

Array Pagination

ArrayPaginator is a simple pagination for array. You can use method Next() to iterate through all pages.

package main

import (
	"fmt"
	"github.com/gopi-frame/pagination"
)

func main() {
	numbers := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
	pagination.NewArray[int](numbers, 1, 5)
	fmt.Println(paginator.Items()) // [1, 2, 3, 4, 5]
	fmt.Println(paginator.LastPage()) // 2
	fmt.Println(paginator.Total()) // 10
	// iterate
	for pagigator.Next() {
		// do something
    }
}

Lazy Load Pagination

LazyPaginator is a pagination for lazy load data. You can use method Next() to iterate through all pages.

package main

import "github.com/gopi-frame/pagination"

func main() {
	numbers := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
	var loader = func(page, pageSize int) ([]int, int64) {
		total := int64(len(numbers))
		s := (page - 1) * pageSize
		if int64(s) >= total {
			return []int{}, total
		}
		e := page * pageSize
		if e >= total {
			return numbers[s:], total
		}
		return numbers[s:e], total
	}
	paginator := pagination.NewLazy(loader, 10, 1)
	// iterate
	for pagigator.Next() {
		// do something
	}
}

FAQs

Package last updated on 05 Aug 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc