Popularity
0.3
Declining
Activity
0.0
Stable
4
0
0

Description

A simple package to paginate your data in Go.

Programming language: Go
License: MIT License
Tags: Utilities     Go     Golang     Pagination     Paginator     Paging    

paginator alternatives and similar packages

Based on the "Utilities" category.
Alternatively, view paginator alternatives based on common mentions on social networks and blogs.

Do you think we are missing an alternative of paginator or a related project?

Add another 'Utilities' Package

README

paginator GoDoc

A simple package to paginate your data in Go.

Installation

$ go get github.com/yassinouider/paginator

Usage

In your handler

//This will automatically read the URL query parameter "page" and "per_page" in the request r, 
//if they are not present the default values will be chosen
p := paginator.New(r)

Example Server

package main 

import "github.com/yassinouider/paginator"

func main() {
    http.HandleFunc("/customers", func(w http.ResponseWriter, r *http.Request) {
        p := paginator.New(r)

        customers, total, _ := getFakeDataStore().FindCustomers(p.Limit(), p.Offset())

        res := map[string]interface{}{
            "data":       customers,
            "pagination": p.SetCount(len(customers)).SetTotal(total),
        }

        b, _ := json.MarshalIndent(res, "", " ")

        w.Header().Set("Content-Type", "application/json")
        w.Write(b)
    })

    http.ListenAndServe(":8080", nil)
}

Example Client

resp, err := http.Get("http://localhost:8080/customers?page=2&per_page=2")
if err != nil {
    panic(err)
}

defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
    panic(err)
}

fmt.Println(string(body))

Output

{
  "data": [
    "User 5",
    "User 6"
  ],
  "pagination": {
    "total": 7,
    "count": 2,
    "per_page": 2,
    "current_page": 3,
    "total_page": 4
  }
}

You can change the default values just like this

//default value = 50
paginator.PerPage = 20

//default value = 1000
paginator.PerPageMax = 100 

//default value = "page"
paginator.PageName = "p"// http://localhost:8080/customers?p=3

//default value = "per_page"
paginator.PerPageName = "limit" // http://localhost:8080/customers?limit=5