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!!")
}
This code displays 2 "successfull Operation!!" but when I add /home
(http.HandleFunc("/home", foo)
), it doesn't. I want to know why it displays "successfull Operation!!" twice. Thank you in advance.
Because modern browsers send an extra request for /favicon.ico
which is also handled in your /
request handler.
If you ping your server with curl
for example, you'll see only one request being sent:
curl localhost:3000