在Windows中不稳定的行为? [英] Erratic behavior in Windows?

查看:153
本文介绍了在Windows中不稳定的行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 更新:问题标题可能会引起误解。这根本不是Go的错。 
查看第一条评论或接受的答案。

下面的代码(好吧,几乎相同)统计在Linux下的页面浏览量,在Windows下。



有人可以找出原因吗?

  package main 
$ b b导入(
fmt
http


func main(){
println(Running )
http.HandleFunc(/,makeHomeHandler())
http.ListenAndServe(:8080,nil)
}

//此版本在Linux版本的Golang下编译并运行OK
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 ++
}
}

/ *
//此版本在Golang的mingw版本下编译并运行OK
//它使用不同的参数类型对于处理程序函数
//但计数器表示迄今为止1,然后迄今为止3,5 s远等等。
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 =>计数猴子,迄今为止。 
http:// localhost:8080 / monkeys =>计数猴子,3到目前为止。
http:// localhost:8080 / donkeys =>计数驴5,到目前为止。

这可能是一个错误吗?



跟进:

实际上,如果我为另一个页面定义了一个额外的处理程序,如:

  http.HandleFunc(/ test,testPageHandler)

没有一个关闭,也不增加任何东西,反正会增加反正,但只有+1:

所以,仍然在Mingw下:

  http:// localhost:8080 / monkeys =>计数猴子,迄今为止。 
http:// localhost:8080 / monkeys =>计数猴子,3到目前为止。
http:// localhost:8080 / test =>另一个不增加视图的处理函数
http:// localhost:8080 / donkeys =>计数驴,迄今为止6。

在Linux下,输出如下:

  http:// localhost:8080 / monkeys =>计数猴子,迄今为止。 
http:// localhost:8080 / monkeys =>计数猴子,迄今为止。
http:// localhost:8080 / test =>另一个不增加视图的处理函数
http:// localhost:8080 / donkeys =>计数驴,3到目前为止。


解决方案

我怀疑您看到的行为是由于到浏览器请求您的网页的图标,而不是由于Windows / mingw。如果您想知道,我使用的是6g ond Darwin,我的Firefox 3.6也在Mac OS X上运行。



为了强调我的怀疑,请尝试将以下内容添加到处理函数:

  fmt.Printf(Counting%s,%d so far.\\\
,r.URL。路径[1:],视图)

然后您可以看到到达您的应用程序的所有请求。从新开始的Firefox到URL http:// localhost:8080 / chuchichaestli 的一个请求会产生以下输出:

 计数chuchichaestli,迄今为止。 
统计favicon.ico,目前为止2个。

,因为Firefox也会为您的页面请求图标。



此外,即使可能存在多个并发请求,您也不会锁定/同步对 views 的访问。


  Update:  The question title can be misleading.  This was not Go's fault at all.
           See the first comment or the accepted answer.

This following code (well, almost the same) counts page views under Linux allright, but counts them double under Windows.

Can someone figure out why?

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++
    }
}
*/

Under 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.

Could this be a bug?

Followup:

In fact, if I define an additional handler for another page, like:

   http.HandleFunc("/test", testPageHandler)

Wich does not have a closure, nor increments anything, the counter gets incremented anyway, but only +1:

So, still under 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.

Under Linux output is as spected:

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.

解决方案

I suspect that the behaviour you're seeing is due to the browser requesting a favicon for your page and not due to Windows/mingw. In case you wonder, I'm using 6g ond Darwin, my Firefox 3.6 is also running on Mac OS X.

To underline my suspicion, try adding the following to the handler function:

fmt.Printf("Counting %s, %d so far.\n", r.URL.Path[1:], views)

Then you can see all the requests reaching your application. One request from a freshly started Firefox to the URL http://localhost:8080/chuchichaestli yields this output:

Counting chuchichaestli, 1 so far.
Counting favicon.ico, 2 so far.

because Firefox also requests the favicon for your go page.

Furthermore you're not locking/synchronising access to views even though there could be multiple concurrent requests.

这篇关于在Windows中不稳定的行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆