go语言分页

package main
import (
    "fmt"
    "strconv"
)
func main() {
    var page=pagestr("http://www.abc.com?",195,11,10,8)
    fmt.Println(page)
}
//url连接,countdatas表数据总行数,currentpage第几页(0是第一页),everpage每页多少行,beforlaterln前后长度
func pagestr(url string,countdatas int,currentpage int,everpage int,beforlaterln int) string {
    var page= ""
    var intpart = countdatas / everpage //整数
    //fmt.Println("intpart")
    //fmt.Println(intpart)
    var remainderpart = countdatas%everpage //余数
    var countpage = intpart //取整数
    if(currentpage<0){ currentpage = 0 } else {currentpage = currentpage}
    if(remainderpart==0){
        if(currentpage>(countpage-1)){ currentpage = countpage-1 } else {currentpage = currentpage}
        var pp=currentpage+1
            var strbe=""
            var pagebefore=pp-beforlaterln-1
            for i := pp; i > pagebefore; i-- {
                if(i>0 && i"+strconv.Itoa((i))+""+strbe
                }
            }
            if(pp<=1){strbe=""}else{strbe="首页"+"上一页"+strbe}
            var strmid = ""+strconv.Itoa(pp)+""
            var strlater = ""
            var pagelater = pp+beforlaterln
            for i := pp; i < pagelater; i++ {
                if(i"+strconv.Itoa((i+1))+"";
                }
            }

            if(pp>=countpage){}else{strlater+="下一页"+"尾页"}
            page=strbe+strmid+strlater;
    }else{
        countpage+=1;//总页数
        if(currentpage>(countpage-1)){ currentpage = countpage-1 } else {currentpage = currentpage}
        var pp=currentpage+1
        var strbe=""
        var pagebefore=pp-beforlaterln-1
        for i := pp; i > pagebefore; i-- {
            if(i>0 && i"+strconv.Itoa((i))+""+strbe
            }
        }
        if(pp<=1){strbe=""}else{strbe="首页"+"上一页"+strbe}
        var strmid = ""+strconv.Itoa(pp)+""
        var strlater = ""
        var pagelater = pp+beforlaterln
        for i := pp; i < pagelater; i++ {
            if(i"+strconv.Itoa((i+1))+"";
            }
        }

        if(pp>=countpage){}else{strlater+="下一页"+"尾页"}
        page=strbe+strmid+strlater;
    }
    return  "共"+strconv.Itoa(countpage)+"页"+page
}

运行后得到一下字符串

共20页首页
上一页
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
下一页
尾页

希望对大家有帮助,谢谢!

你可能感兴趣的:(go语言分页)