
我正在阅读《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 页面时增加,并且会根据我刷新的次数而增加。
那是因为当你用浏览器测试网页时,大多数时候,浏览器也会向http://localhost:8000/favicon.ico发送请求。请参阅下面的屏幕截图:
/favicon.ico 没有专用的处理程序,它与 / 匹配,因此将由 server.handler 处理。
建议使用其他工具来测试此类演示。例如,curl:
$ curl 'http://localhost:8000/' $ curl 'http://localhost:8000/count'
以上就是Go编程语言书上的例子server2错了?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号