适用于你的 Go Web Server,包装你的请求,提供分页信息的解析。
特色:
用法:
pg := pagination.DefaultPagination()
pgt := pg.Parse(requestURI)
paginatedData := pgt.WrapWithTruncate(TrunctableBooks(books), total)
responseBody, _ := json.MarshalIndent(paginatedData, "", " ")
fmt.Println(string(responseBody))
offset, length := pgt.GetOffsetRange()
total, items := db.Offset(offset).Limit(length).Query()
start, end := pgt.GetRange()
total, items := db.QueryAll()
items = items[start:end]
项目地址: https://github.com/zheeeng/pagination
求万能的 V 友的 star ~
样例输出
{
"pagination": {
"page": 2,
"page_size": 5,
"total": 20,
"first": "api.example.com/books?author=jk&page=1&page_size=5",
"last": "api.example.com/books?author=jk&page=4&page_size=5",
"prev": "api.example.com/books?author=jk&page=1&page_size=5",
"next": "api.example.com/books?author=jk&page=3&page_size=5",
"query": {
"author": [
"jk"
],
"page": [
"2"
],
"page_size": [
"5"
]
}
},
"result": [
{
"id": 5,
"author": "jk",
"name": "book"
},
{
"id": 6,
"author": "jk",
"name": "book"
},
{
"id": 7,
"author": "jk",
"name": "book"
},
{
"id": 8,
"author": "jk",
"name": "book"
},
{
"id": 9,
"author": "jk",
"name": "book"
}
]
}