Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Windows中的不稳定行为?_Windows_Go_Mingw - Fatal编程技术网

Windows中的不稳定行为?

Windows中的不稳定行为?,windows,go,mingw,Windows,Go,Mingw,下面的代码(几乎相同)计算Linux下的页面浏览量,但Windows下的页面浏览量是原来的两倍 有人能找出原因吗 Update: The question title can be misleading. This was not Go's fault at all. See the first comment or the accepted answer. 在Mingw下: package main import ( "fmt" "http" )

下面的代码(几乎相同)计算Linux下的页面浏览量,但Windows下的页面浏览量是原来的两倍

有人能找出原因吗

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

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++
    }
}
*/
   http.HandleFunc("/test", testPageHandler)
这可能是一只虫子吗

后续行动:

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

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.
Wich没有闭包,也不增加任何内容,计数器无论如何都会增加,但只增加+1:

因此,仍然在Mingw下:

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++
    }
}
*/
   http.HandleFunc("/test", testPageHandler)
Linux下的输出如下所示:

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.

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

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

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.
然后您可以看到所有到达应用程序的请求。新启动的Firefox对URL的一个请求产生以下输出:

fmt.Printf("Counting %s, %d so far.\n", r.URL.Path[1:], views)
因为Firefox也会为你的go页面请求favicon


此外,即使可能存在多个并发请求,您也没有锁定/同步对
视图的访问。

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

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

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.
然后您可以看到所有到达应用程序的请求。新启动的Firefox对URL的一个请求产生以下输出:

fmt.Printf("Counting %s, %d so far.\n", r.URL.Path[1:], views)
因为Firefox也会为你的go页面请求favicon


此外,您没有锁定/同步对
视图的访问,即使可能存在多个并发请求。

解决了!这不是Go的错。我的基于Windows的浏览器在每次请求时都会不断请求favicon.ico。谢谢@distributed!解决了的!这不是Go的错。我的基于Windows的浏览器在每次请求时都会不断请求favicon.ico。谢谢@distributed!如何锁定对
视图的访问权限
?如上所述,此代码不安全,因为它在读取/写入计数器时存在数据竞争。正确的方法是使用类似或的东西。没有。如何锁定对
视图的访问权限
?如上所述,此代码不安全,因为它在读取/写入计数器时存在数据竞争。正确的方法是使用类似或的东西。没有。