【发布时间】:2016-01-30 15:43:07
【问题描述】:
我在 Go 中关注 simple web server example。
我插入了log 语句,结果代码如下所示:
package main
import (
"io"
"log"
"net/http"
)
func hello(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, "Hello world!")
log.Println("hello.")
}
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/", hello)
http.ListenAndServe(":8000", mux)
}
问题是每当我在我的网络浏览器中加载端口 8000 时,这个函数都会被调用两次。这是一个问题,因为我打算在每次页面访问时增加一个计数器。通过这种行为,计数器会增加两次。 OTOH,如果我这样做curl localhost:8000,它只会被调用一次。
我觉得我在这里错过了一些非常愚蠢的事情。
【问题讨论】:
标签: go