go语言web项目翻页实现(改写自php)

31 views
Skip to first unread message

叶新伟

unread,
Mar 6, 2014, 8:49:07 PM3/6/14
to golang...@googlegroups.com
package main
import (
"strings"
"strconv"
"math"
"fmt"
"os"

)
func main() {
var html string
html= multi(100, 10, 3, "http://xxoo.io")

userFile := "log.html"
        fout, _ := os.Create(userFile)
defer fout.Close()

fmt.Println(html)
fout.WriteString(html)


}


func multi(num int, perpage int, curpage int, url string) (html string) {
var (
mpurl string
from  int
to    int
ret   []byte
)

ret= []byte("")

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= []byte(string(ret)+ "<a href=\""+ mpurl + "page=1\" class=\"p_redirect\">&laquo;</a>")
}

if curpage > 1 {
ret= []byte(string(ret)+ "<a href=\""+ mpurl + "page="+ strconv.Itoa(curpage - 1)+ "\" class=\"p_redirect\">&#8249;</a>")
}


for i := from; i <= to; i++  {
if i == curpage {
ret= []byte(string(ret)+ "<span class=\"p_curpage\">" + strconv.Itoa(i)+ "</span>")
}else{
ret= []byte(string(ret)+ "<a href=\""+ mpurl+ "page="+ strconv.Itoa(i) + "\" class=\"p_num\">"+ strconv.Itoa(i) + "</a>")
}
}

if curpage < pages {
ret= []byte(string(ret)+ "<a href=\""+ mpurl + "page="+ strconv.Itoa(curpage + 1)+ "\" class=\"p_redirect\">&#8250;</a>")
}

if to < pages {
ret= []byte(string(ret)+ "<a href=\""+ mpurl + "page="+ strconv.Itoa(pages)+ "\" class=\"p_redirect\">&raquo;</a>")
}

if string(ret)!= "" {
ret= []byte("<div class=\"p_bar\"><span class=\"p_info\">Records:"+ strconv.Itoa(num) + "</span>"+ string(ret) + "</div>")
}

}
return string(ret)

}


/*
function multi($num, $perpage, $curpage, $mpurl) {
$multipage = '';
$mpurl .= strpos($mpurl, '?') ? '&amp;' : '?';
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">&laquo;</a>' : '').($curpage > 1 ? '<a href="'.$mpurl.'page='.($curpage - 1).'" class="p_redirect">&#8249;</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">&#8250;</a>' : '').($to < $pages ? '<a href="'.$mpurl.'page='.$pages.'" class="p_redirect">&raquo;</a>' : '');
$multipage = $multipage ? '<div class="p_bar"><span class="p_info">Records:'.$num.'</span>'.$multipage.'</div>' : '';
}
return $multipage;
}

*/


minux

unread,
Mar 6, 2014, 9:16:58 PM3/6/14
to golang...@googlegroups.com
建议下次发代码之前先按照惯例 gofmt 一下。
我假设发代码的目的是让人提出建议。

On Thu, Mar 6, 2014 at 8:49 PM, 叶新伟 <yxw.2...@gmail.com> wrote:
package main
import (
"strings"
"strconv"
"math"
"fmt"
"os"

)
func main() {
var html string
html= multi(100, 10, 3, "http://xxoo.io")
html := 会更好些,multi 这个名字不够好,不知道是要做什么的函数。 

userFile := "log.html"
        fout, _ := os.Create(userFile)
忽略返回错误? 
defer fout.Close()

fmt.Println(html)
fout.WriteString(html)
}


func multi(num int, perpage int, curpage int, url string) (html string) {
如果加上注释的话,就完全没必要给返回值命名。
// multi .... , and returns the HTML content as a string.
另外 出错的情况呢?至少应该是 (string, error) 吧。
还有,如果大量使用字符串拼接的话,不如返回 []byte,然后在函数体里面用 bytes.Buffer。

这个程序通篇没有考虑错误处理,这是个很要命的问题。 

另外 变量的命名应该是 mixedCap。
var (
mpurl string
from  int
to    int
ret   []byte
既然你用 []byte,为啥不直接返回 []byte 呢?需要注意到 []string 到 string 的转换一般是要
复制一遍的。(取决于编译器,以后编译器应该会优化这里这种情况,但是代码不应该依赖
这种优化,尤其是调用者还是要写到 Writer 里的) 
)

ret= []byte("")

if strings.Index(url, "?")!= -1 {
mpurl = url+ "&amp;"
}else{
mpurl = url+ "?;"
}
你确定要自己构造 URL 而不是用 net/url 包么? 


if num> perpage {
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) {
还是那句话,一定要 gofmt 你的代码。 
from = curpage - pages + to
to = pages
if((to - from) < page && (to - from) < pages) {
from = pages - page + 1
}
}
}
if (curpage - offset) > 1 && pages > page {
ret= []byte(string(ret)+ "<a href=\""+ mpurl + "page=1\" class=\"p_redirect\">&laquo;</a>")
直接拼接 HTML,没有 escape?
建议用 html/template 组合这个。至少也得用 html.EscapeString 处理一下。
}

if curpage > 1 {
ret= []byte(string(ret)+ "<a href=\""+ mpurl + "page="+ strconv.Itoa(curpage - 1)+ "\" class=\"p_redirect\">&#8249;</a>") 
}

for i := from; i <= to; i++  {
if i == curpage {
ret= []byte(string(ret)+ "<span class=\"p_curpage\">" + strconv.Itoa(i)+ "</span>")
}else{
ret= []byte(string(ret)+ "<a href=\""+ mpurl+ "page="+ strconv.Itoa(i) + "\" class=\"p_num\">"+ strconv.Itoa(i) + "</a>")
}
}

if curpage < pages {
ret= []byte(string(ret)+ "<a href=\""+ mpurl + "page="+ strconv.Itoa(curpage + 1)+ "\" class=\"p_redirect\">&#8250;</a>")
}

if to < pages {
ret= []byte(string(ret)+ "<a href=\""+ mpurl + "page="+ strconv.Itoa(pages)+ "\" class=\"p_redirect\">&raquo;</a>")
}

if string(ret)!= "" {
为啥不用 len(ret) > 0 ? 
原来是是从 PHP 直接翻译的……


总结:建议看看 Effective Go 和 https://code.google.com/p/go-wiki/wiki/CodeReviewComments 

alex.wq

unread,
Mar 6, 2014, 10:15:15 PM3/6/14
to golang...@googlegroups.com
呵呵,不错


--
--
官网: http://golang-china.org/
IRC: irc.freenode.net #golang-china
@golangchina
---
您收到此邮件是因为您订阅了Google网上论坛中的“Golang-China”论坛。
要取消订阅此论坛,并停止接收其发来的电子邮件,请发送电子邮件至golang-china...@googlegroups.com
要在网络上查看此讨论,请访问 https://groups.google.com/d/msgid/golang-china/CA%2Bdb%3Dn0Uu4K_ryNGaSTEDucbqTdbakmteyjzrAOjm6WfQhDvrA%40mail.gmail.com
如需了解更多选项,请访问https://groups.google.com/d/optout

叶新伟

unread,
Mar 6, 2014, 10:36:55 PM3/6/14
to golang...@googlegroups.com
是直接翻译的!



在 2014年3月6日星期四UTC-8下午5时49分07秒,叶新伟写道:
Reply all
Reply to author
Forward
0 new messages