翻译自php的一个分页函数,能用!代码写得比较垃圾,抽空再改进。
package main import ( "bytes" "fmt" "math" "os" "strconv" "strings" ) func main() { var html string html = multi(100, 10, 3, "http://xxoo.io") userFile := "log.html" fout, _ := os.Create(userFile) fmt.Println(html) fout.WriteString(html) fout.Close() } func multi(num int, perpage int, curpage int, url string) string { var ( mpurl string from int to int //ret *bytes.Buffer = new(bytes.Buffer) ret bytes.Buffer = bytes.Buffer{} ) if strings.Index(url, "?") != -1 { mpurl = url + "&" } else { mpurl = url + "?;" } if num > perpage { page := 10 offset := 5 pages := int(math.Ceil(float64(num / perpage))) if page > pages { from = 1 to = pages } else { from = curpage - offset to = curpage + page + offset - 1 if from < 1 { to = curpage + 1 - from from = 1 if (to-from) < page && (to-from) < pages { to = page } } else if to > pages { from = curpage - pages + to to = pages if (to-from) < page && (to-from) < pages { from = pages - page + 1 } } } if (curpage-offset) > 1 && pages > page { ret.WriteString("<a href=\"" + mpurl + "page=1\" class=\"p_redirect\">«</a>") } if curpage > 1 { ret.WriteString("<a href=\"" + mpurl + "page=" + strconv.Itoa(curpage-1) + "\" class=\"p_redirect\">‹</a>") } for i := from; i <= to; i++ { if i == curpage { ret.WriteString("<span class=\"p_curpage\">" + strconv.Itoa(i) + "</span>") } else { ret.WriteString("<a href=\"" + mpurl + "page=" + strconv.Itoa(i) + "\" class=\"p_num\">" + strconv.Itoa(i) + "</a>") } } if curpage < pages { ret.WriteString("<a href=\"" + mpurl + "page=" + strconv.Itoa(curpage+1) + "\" class=\"p_redirect\">›</a>") } if to < pages { ret.WriteString("<a href=\"" + mpurl + "page=" + strconv.Itoa(pages) + "\" class=\"p_redirect\">»</a>") } if ret.Len() > 0 { return "<div class=\"p_bar\"><span class=\"p_info\">Records:" + strconv.Itoa(num) + "</span>" + ret.String() + "</div>" } } return "" } /* function multi($num, $perpage, $curpage, $mpurl) { $multipage = ''; $mpurl .= strpos($mpurl, '?') ? '&' : '?'; if($num > $perpage) { $page = 10; $offset = 5; $pages = @ceil($num / $perpage); if($page > $pages) { $from = 1; $to = $pages; } else { $from = $curpage - $offset; $to = $curpage + $page - $offset - 1; if($from < 1) { $to = $curpage + 1 - $from; $from = 1; if(($to - $from) < $page && ($to - $from) < $pages) { $to = $page; } } elseif($to > $pages) { $from = $curpage - $pages + $to; $to = $pages; if(($to - $from) < $page && ($to - $from) < $pages) { $from = $pages - $page + 1; } } } $multipage = ($curpage - $offset > 1 && $pages > $page ? '<a href="'.$mpurl.'page=1" class="p_redirect">«</a>' : '').($curpage > 1 ? '<a href="'.$mpurl.'page='.($curpage - 1).'" class="p_redirect">‹</a>' : ''); for($i = $from; $i <= $to; $i++) { $multipage .= $i == $curpage ? '<span class="p_curpage">'.$i.'</span>' : '<a href="'.$mpurl.'page='.$i.'" class="p_num">'.$i.'</a>'; } $multipage .= ($curpage < $pages ? '<a href="'.$mpurl.'page='.($curpage + 1).'" class="p_redirect">›</a>' : '').($to < $pages ? '<a href="'.$mpurl.'page='.$pages.'" class="p_redirect">»</a>' : ''); $multipage = $multipage ? '<div class="p_bar"><span class="p_info">Records:'.$num.'</span>'.$multipage.'</div>' : ''; } return $multipage; } */
有疑问加站长微信联系(非本文作者)