英文:
The Go Programming Language book example server2 is wrong?
问题
我正在阅读《Go语言圣经》这本书。在第1章的第2个服务器示例中,使用了互斥锁来防止竞态条件。然而,我复制了代码并尝试运行它,结果得到了不一致的结果。示例中的代码有问题吗?
以下是我使用的代码:
server.go
package server
import (
"fmt"
"log"
"net/http"
"sync"
)
const (
PORT string = ":8000"
)
var count int
var mu sync.Mutex
func Run() {
http.HandleFunc("/", handler)
http.HandleFunc("/count", counter)
fmt.Printf("Server is listening on port: %s\n", PORT)
log.Fatal(http.ListenAndServe(PORT, nil))
}
func handler(w http.ResponseWriter, r *http.Request) {
mu.Lock()
count++
mu.Unlock()
fmt.Fprintf(w, "URL Path = %q\n", r.URL.Path)
}
func counter(w http.ResponseWriter, r *http.Request) {
mu.Lock()
fmt.Fprintf(w, "Count = %d\n", count)
mu.Unlock()
}
main.go
package main
import "book/server"
func main() {
server.Run()
}
当我运行go run main.go
并访问两个页面localhost:8000
和localhost:8000/count
时:
- 每当我刷新
/count
页面时,计数会增加。为什么? - 每当我刷新
/
和/count
页面时,显示的计数增加是不一致的?不符合刷新次数。为什么?
我原本期望只有在访问/
页面时计数才会增加,而不是在访问/count
页面时增加,并且计数会根据我刷新的次数增加。
英文:
I am going through The Go programming Language Book. In chapter 1, server 2 example: Book's code the mutex is used to prevent the race condition. However i copied the code and tried to run it and it's giving inconsistent result. Is the code in the example wrong?
Here's how i used the code:
server.go
package server
import (
"fmt"
"log"
"net/http"
"sync"
)
const (
PORT string = ":8000"
)
var count int
var mu sync.Mutex
func Run() {
http.HandleFunc("/", handler)
http.HandleFunc("/count", counter)
fmt.Printf("Server is listening on port: %s\n", PORT)
log.Fatal(http.ListenAndServe(PORT, nil))
}
func handler(w http.ResponseWriter, r *http.Request) {
mu.Lock()
count++
mu.Unlock()
fmt.Fprintf(w, "URL Path = %q\n", r.URL.Path)
}
func counter(w http.ResponseWriter, r *http.Request) {
mu.Lock()
fmt.Fprintf(w, "Count = %d\n", count)
mu.Unlock()
}
main.go
package main
import "book/server"
func main() {
server.Run()
}
When i run: go run main.go and visit the two pages localhost:8000 and localhost:8000/count
- Whenever I refresh the /count page the count increases. Why?
- Whenever I refresh the / and /count page the count shown are inconsistently increased? Not according to the number of refreshes. Why?
I was expecting the count would only increase when i visit / page and not /count page and it would increase according to the number of refreshes I did.
答案1
得分: 6
这是因为当你用浏览器测试网页时,大部分情况下,浏览器也会发送一个请求到 http://localhost:8000/favicon.ico
。请参考下面的截图:
没有专门处理 /favicon.ico
的处理程序,它与 /
匹配,所以会由 server.handler
处理。
建议使用其他工具来测试这种类型的演示。例如,使用 curl
命令:
$ curl 'http://localhost:8000/'
$ curl 'http://localhost:8000/count'
英文:
That's because when you test the web page with a browser, most of the time, the browser will send a request to http://localhost:8000/favicon.ico
too. See the screenshot below:
There is not a dedicated handler for /favicon.ico
, and it matches /
, so it will be handled by server.handler
.
It's recommended to use other tools for testing such kind of demos. For example, curl
:
$ curl 'http://localhost:8000/'
$ curl 'http://localhost:8000/count'
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论