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<pp){
strbe = "<a class='num' href='"+url+"&p="+strconv.Itoa((i-1))+"'>"+strconv.Itoa((i))+"</a>"+strbe
}
}
if(pp<=1){strbe=""}else{strbe="<a class='num' href='"+url+"&p=0'>首頁</a>"+"<a class='num' href='"+url+"&p="+strconv.Itoa((pp-2))+"'>上一頁</a>"+strbe}
var strmid = "<span class='current'>"+strconv.Itoa(pp)+"</span>"
var strlater = ""
var pagelater = pp+beforlaterln
for i := pp; i < pagelater; i++ {
if(i<countpage){
strlater+="<a class='num' href='"+url+"&p="+strconv.Itoa(i)+"'>"+strconv.Itoa((i+1))+"</a>";
}
}
if(pp>=countpage){}else{strlater+="<a class='num' href='"+url+"&p="+strconv.Itoa(pp)+"'>下一頁</a>"+"<a class='num' href='"+url+"&p="+strconv.Itoa((countpage-1))+"'>尾頁</a>"}
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<pp){
strbe = "<a class='num' href='"+url+"&p="+strconv.Itoa((i-1))+"'>"+strconv.Itoa((i))+"</a>"+strbe
}
}
if(pp<=1){strbe=""}else{strbe="<a class='num' href='"+url+"&p=0'>首頁</a>"+"<a class='num' href='"+url+"&p="+strconv.Itoa((pp-2))+"'>上一頁</a>"+strbe}
var strmid = "<span class='current'>"+strconv.Itoa(pp)+"</span>"
var strlater = ""
var pagelater = pp+beforlaterln
for i := pp; i < pagelater; i++ {
if(i<countpage){
strlater+="<a class='num' href='"+url+"&p="+strconv.Itoa(i)+"'>"+strconv.Itoa((i+1))+"</a>";
}
}
if(pp>=countpage){}else{strlater+="<a class='num' href='"+url+"&p="+strconv.Itoa(pp)+"'>下一頁</a>"+"<a class='num' href='"+url+"&p="+strconv.Itoa((countpage-1))+"'>尾頁</a>"}
page=strbe+strmid+strlater;
}
return "共"+strconv.Itoa(countpage)+"頁"+page
}
運行后得到一下字符串
共20頁<a class='num' >首頁</a>
<a class='num' >上一頁</a>
<a class='num' >4</a>
<a class='num' >5</a>
<a class='num' >6</a>
<a class='num' >7</a>
<a class='num' >8</a>
<a class='num' >9</a>
<a class='num' >10</a>
<a class='num' >11</a>
<span class='current'>12</span>
<a class='num' >13</a>
<a class='num' >14</a>
<a class='num' >15</a>
<a class='num' >16</a>
<a class='num' >17</a>
<a class='num' >18</a>
<a class='num' >19</a>
<a class='num' >20</a>
<a class='num' >下一頁</a>
<a class='num' >尾頁</a>
希望對大家有幫助癣猾,謝謝碰酝!