Total Pageviews

Saturday, 13 August 2016

go的一个Pagination(分页)程序

Simple pagination for go, with HTML support.
 Coverage Godoc license
Pagination is a simple package that provides pagination. Has HTML support using the html/template package.

Installation

Install using go get.
$ go get github.com/SayonAB/pagination
Then import it in your project.
import "github.com/SayonAB/pagination"

Usage

Create a basic pagination and check the offset for current page
numPosts := db.Posts().Count()
postsPerPage := 25
currentPage := request.Query().Int("page")
p := pagination.New(numPosts, postsPerPage, currentPage)
fmt.Printf("The current offset is: %d\n", p.Offset()) // The current offset is: 75
Create a HTML pagination and use it in a html/template
p := pagination.NewHTML(110, 25, 2)
data := map[string]interface{}{
  "Pager": p,
}
var out bytes.Buffer
t := template.Must(template.New("pagination-test").Parse("{{ .Pager.Render }}"))
t.Execute(&out, data)
fmt.Printf("HTML list: %s\n", out.String())

Testing

Run the tests using go test.
$ go test
from https://github.com/SayonAB/pagination