tags:

views:

56

answers:

1

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.
+4  A: 

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.

distributed
Good advice. You are right about the favicon issue.
Sebastián Grignoli