我正在遵循书中的示例。它假设在键入http://localhost:8080/xxx时增加计数并在键入http://localhost:8080/ count 时返回计数
代码如下
var count int
var mu sync.Mutex
func main() {
http.HandleFunc("/", handler)
http.HandleFunc("/count", counter)
log.Fatal(http.ListenAndServe("localhost:8080", nil))
}
func handler(w http.ResponseWriter, req *http.Request) {
mu.Lock()
fmt.Fprintf(w, "Count before: %d\n", count)
count++
fmt.Fprintf(w, "Count after: %d\n", count)
mu.Unlock()
fmt.Fprintf(w, "URL.Path = %q\n", req.URL.Path)
}
func counter(w http.ResponseWriter, req *http.Request) {
mu.Lock()
fmt.Fprintf(w, "Count: %d\n", count)
mu.Unlock()
}
但是,当我打开http://localhost:8080并刷新时,计数每次增加 2 而不是 1。这是 Chrome 中的某些功能吗?
慕姐4208626
相关分类