Skip to content

Latest commit

 

History

History
100 lines (72 loc) · 1.79 KB

README.md

File metadata and controls

100 lines (72 loc) · 1.79 KB

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