func main() {
http.HandleFunc("/", foo)
http.ListenAndServe(":3000", nil)
}
func foo(w http.ResponseWriter, r *http.Request) {
s:= "name"
fp := path.Join("templates", "index.html")
tmpl, err := template.ParseFiles(fp)
if err != nil {
panic(err)
}
if err := tmpl.Execute(w, s); err != nil {
panic(err)
}
fmt.Println("successfull Operation!!")
}
此代码显示2“成功操作!!”但是当我添加/home
(http.HandleFunc("/home", foo)
)时,却没有。我想知道为什么它显示“成功操作!!”两次。提前谢谢。
答案 0 :(得分:5)
因为现代浏览器会发送/favicon.ico
的额外请求,这也会在您的/
请求处理程序中处理。
例如,如果您使用curl
ping服务器,则只会看到一个请求被发送:
curl localhost:3000