为什么每次打开网页都要GET两次

weixin_38050602 2019-09-20 10:55:31
```go
package main

import (
"fmt"
"net/http"
"html/template"
)

var count int = 1

func demoHandler(w http.ResponseWriter, r *http.Request) {
t, err := template.New("demo").Parse("htllo {{.}}")
if err != nil {fmt.Println(err)}

fmt.Println(r.Method, count)
t.Execute(w, count)
count++
return
}

func demo() {
http.HandleFunc("/", demoHandler)
http.ListenAndServe(":8080", nil)
}

func main() {
demo()
}
```

每次打开/刷新网页都要GET两次,网页显示不是预期中的hello 1,2,3,4,5 ,而是hello 1,3,5,7,9,具体如下:

###### win10 cmd 里 go run执行代码,打开127.0.0.1:8080
---
网页显示:

```go
htllo 1
```

cmd显示:
```go
D:\go\src>go run ceshizhuanyong.go
GET 1
GET 2
```
---
刷新网页

网页显示:

```go
htllo 3
```

cmd显示:
```go
D:\go\src>go run ceshizhuanyong.go
GET 1
GET 2
GET 3
GET 4
```
---
一直刷新,网页显示hello 1,3,5,7,9,把t.Execute放在count++后面,网页显示hello 2,4,6,8,10,这样做好像没有任何意义哈~
网上找了一下,没找到答案,刚开始学,没有基础,只学过一点js,有哪位大佬回答问题请写的详细一些,如果没有时间写太详细请注明一个方向,我有足够多去查资料,感谢
...全文
114 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
weixin_38058113 2019-09-20
  • 打赏
  • 举报
回复
因为浏览器还请求了 favicon
weixin_38069981 2019-09-20
  • 打赏
  • 举报
回复
随便做了个demo.html和favicon.ico,放在.go文件同目录下(src) ```html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <link rel="icon" href="favicon.ico" /> <title>demo</title> </head> <body> count = {{.}} </body> </html> ``` 然后 `t, err := template.New("demo").Parse("htllo {{.}}")` --> `t, err := template.ParseFiles("demo.html")` 还是GET两次,浏览器能不能一次把demo.html和favicon.ico都GET到,我先去搜一下~
weixin_38087180 2019-09-20
  • 打赏
  • 举报
回复
解决了吗
weixin_38088281 2019-09-20
  • 打赏
  • 举报
回复
没有,搜不到,感觉不太重要,我要做个小网站,先把架子搭起来,慢慢加东西,先学http和template这两个包了,数据库也不懂,还有好多要学的,不想浪费时间

433

社区成员

发帖
与我相关
我的任务
社区描述
其他技术讨论专区
其他 技术论坛(原bbs)
社区管理员
  • 其他技术讨论专区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧