1024programmer Java How to implement a paging algorithm using golang

How to implement a paging algorithm using golang

How to implement a paging algorithm using golang? In response to this problem, this article introduces the corresponding analysis and answers in detail, hoping to help more friends who want to solve this problem find simpler and easier methods.

The sample code is as follows:

//Paging method, according to the number of pages passed, each  Number of pages, total number, return the paginated content. The format of 7 pages before 1, 2, 3, 4, 5 will be returned. If the number is less than 5 pages, the specific number of pages will be returned.
 func Paginator(page, prepage int, nums int64) map[string]interface{} {

 var firstpage int //Previous page address
 var lastpage int //Next page address
 //Generate the total number of pages based on the total number of nums and the number of prepages per page
 totalpages := int(math.Ceil(float64(nums) / float64(prepage))) //Total pages
 if page > totalpages {
 page = totalpages
 }
 if page = totalpages-5 && totalpages > 5: //Last 5 pages
 start := totalpages - 5 + 1
 firstpage = page - 1
 lastpage = int(math.Min(float64(totalpages), float64(page+1)))
 pages = make([]int, 5)
 for i, _ := range pages {
 pages[i] = start + i
 }
 case page >= 3 && totalpages > 5:
 start := page - 3 + 1
 pages = make([]int, 5)
 firstpage = page - 3
 for i, _ := range pages {
 pages[i] = start + i
 }
 firstpage = page - 1
 lastpage = page + 1
 default:
 pages = make([]int, int(math.Min(5, float64(totalpages))))
 for i, _ := range pages {
 pages[i] = i + 1
 }
 firstpage = int(math.Max(float64(1), float64(page-1)))
 lastpage = page + 1
 //fmt.Println(pages)
 }
 paginatorMap := make(map[string]interface{})
 paginatorMap["pages"] = pages
 paginatorMap["totalpages"] = totalpages
 paginatorMap["firstpage"] = firstpage
 paginatorMap["lastpage"] = lastpage
 paginatorMap["currpage"] = page
 return paginatorMap
 }

The test results are as follows

func main(){
 pageSize := 3
 var rsCount int64 = 100
 currentPage := 8
 res := Paginator(currentPage,pageSize,rsCount)
 fmt.Println(res)
 }

The running results are as follows

map[pages:[6 7 8 9 10] totalpages:34 firstpage:7 lastpage:9 currpage: 8]

The answer to the question about how to implement a paging algorithm using golang is shared here. I hope the above content can be of some help to everyone. If you still have Many doubts have not been solved. You can pay attention to the Programming Notes Industry Information Channel to learn more related knowledge.

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/754386

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索