I am following the example from the book. It supposes to increase the count when type something http://localhost:8080/xxx and return the count number when type http://localhost:8080/count
The code is below
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
", count)
count++
fmt.Fprintf(w, "Count after: %d
", count)
mu.Unlock()
fmt.Fprintf(w, "URL.Path = %q
", req.URL.Path)
}
func counter(w http.ResponseWriter, req *http.Request) {
mu.Lock()
fmt.Fprintf(w, "Count: %d
", count)
mu.Unlock()
}
However, when I open http://localhost:8080 and refresh, count is increase by 2 each time instead of 1. Is this some feature in Chrome?
Chrome automatically tries to fetch the favicon, increasing the counter a second time
You can see it by pressing F12 and going to the "Network" tab before loading the page:
(this is part of the Chrome dev tools: https://developers.google.com/web/tools/chrome-devtools/)
To test that exact code, I'd advise using curl like this : curl -XGET http://localhost:8080/