Update: The question title can be misleading. This was not Go's fault at all.
See the first comment or the accepted answer.
下面的代码(几乎相同)在Linux下可以计算页面浏览量,但在Windows下可以将页面浏览量提高一倍。
有人能弄清楚为什么吗?
package main
import (
"fmt"
"http"
)
func main() {
println("Running")
http.HandleFunc("/", makeHomeHandler())
http.ListenAndServe(":8080", nil)
}
// this version compiles and run OK under the Linux version of Golang
func makeHomeHandler() func(c *http.Conn, r *http.Request) {
views := 1
return func(c *http.Conn, r *http.Request) {
fmt.Fprintf(c, "Counting %s, %d so far.", r.URL.Path[1:], views)
views++
}
}
/*
// this version compiles and run OK under the mingw version of Golang
// it uses a different argument type for the handler function,
// but the counter says "1 so far", then "3 so far", "5 so far", and so on.
func makeHomeHandler() func(c http.ResponseWriter, r *http.Request) {
views := 1
return func(c http.ResponseWriter, r *http.Request) {
fmt.Fprintf(c, "Counting %s, %d so far.", r.URL.Path[1:], views)
views++
}
}
*/
在Mingw下:
http://localhost:8080/monkeys => Counting monkeys, 1 so far.
http://localhost:8080/monkeys => Counting monkeys, 3 so far.
http://localhost:8080/donkeys => Counting donkeys, 5 so far.
这可能是错误吗?
跟进:
实际上,如果我为另一个页面定义了另一个处理程序,例如:
http.HandleFunc("/test", testPageHandler)
Wich没有闭包,也没有增加任何东西,计数器无论如何都会增加,但是只有+1:
因此,仍然在Mingw的领导下:
http://localhost:8080/monkeys => Counting monkeys, 1 so far.
http://localhost:8080/monkeys => Counting monkeys, 3 so far.
http://localhost:8080/test => Another handler function that does not increment "views"
http://localhost:8080/donkeys => Counting donkeys, 6 so far.
在Linux下,输出如下所示:
http://localhost:8080/monkeys => Counting monkeys, 1 so far.
http://localhost:8080/monkeys => Counting monkeys, 2 so far.
http://localhost:8080/test => Another handler function that does not increment "views"
http://localhost:8080/donkeys => Counting donkeys, 3 so far.
有只小跳蛙
相关分类